<?php function say_hello_to($name) { echo "Hey {$name}! <br?>"; } $name = "Me"; say_hello_to($name); //arguments are scooped as local variable function say_hello_with_title($name, $title = "Honorable") { echo " {$title} {$name}"; } $name = "Caro"; $title = "Ms"; say_hello_with_title($name); /*argument order must be maintained prdefined arguments must appear last */ #coder
<pre> <?php # This function returns a multidimensional array containing a list of all defined variables, # be them environment, server or user-defined variables, within the scope that get_defined_vars() is called. # print_r(get_defined_vars()); // get_defined_vars — Returns an array of all defined variables ?> </pre> <br /> <?php function say_hello_to($word) { echo "Hello {$word}!<br /> "; var_dump(debug_backtrace()); // dumps info } say_hello_to('Everyone'); // Third party tools // 1. Xdebug // 2. DBG // 3. FirePHP (firefox) ?> </body> </html>
<!--Define&Call Function--> <?php function say_hello() { echo "Hello_world!<br/>"; } say_hello(); ?> <!--Function arguments--> <?php $name = "John Doe"; function say_hello_to($greeting, $target, $punct) { echo $greeting . " " . $target . $punct . "<br/>"; } say_hello_to("Hello", $name, "!!"); ?> <!--Function return values--> <?php function add($var1 = 8, $var2 = 7) { $sum = $var1 + $var2; return $sum; } $result1 = add(3, 4); echo $result1 . "<br/>"; echo $result2 = add() . "<br/>"; ?> <!--Function multiple return values--> <?php function add_sub($val1, $val2)
<html lang="en"> <head> <title>Functions: Defining</title> </head> <body> <?php function say_hello() { echo "Hello World!<br />"; } say_hello(); function say_hello_to($word) { echo "Hello {$word}!<br />"; } say_hello_to("World"); say_hello_to("Everyone"); say_hello_loudly(); function say_hello_loudly() { echo "HELLO WORLD!<br />"; } // function say_hello_loudly() { // echo "We can't redefine a function."; // } ?> </body> </html>
<head> <meta charset="UTF-8"> <title>Defining Functions</title> </head> <body> <?php // Function: A code that preforms a specific task ex: inarry function say_hello() { echo "Hello World!<br />"; } // Call function // Must use () say_hello(); function say_hello_to($word) { echo "Hello {$word}!<br />"; } say_hello_to("World"); say_hello_to("Friend"); function say_hello_caps() { echo "HELLO"; } say_hello_caps(); // Cannot redefine a function ?> </body> </html>
$sum = $x + $y; return $sum; } // using global variables in functions function use_variable() { global $some_var; echo $some_var; } greetings(); // calling a function echo '<br/>'; echo '<br/>'; greetings_to('John Doe'); // calling function with args echo '<br/>'; echo '<br/>'; say_hello_to('Jane Doe'); echo '<br/>'; echo '<br/>'; echo calculate_sum(15, 68); echo '<br/>'; echo '<br/>'; echo calculate_sum(); echo '<br/>'; echo '<br/>'; echo use_variable(); ?> </body> </html>
<?php function say_hello() { //function without arguments echo "Hey Full Sailors!<br />"; } say_hello(); function say_hello_to($word) { //function with argument echo "Hello {$word}!<br />"; } say_hello_to("Class of 2015"); say_hello_to("SSL Developers"); ?> <br /><br /> <?php say_hello_loudly(); //NOT A BEST PRACTICE // However, this IS allowed because... //PHP Pre-processes your file function say_hello_loudly() { echo "HELLO PROGRAMMING WORLD!<br />"; } //function say_hello_loudly() { //echo "We CANNOT re-define a function."; //} //Uses the same name as a funciton above == NOT ALLOWED ?>