The PHP Language

"; }?>As you can see, the for loop has the three parts separated by semicolons.

In the declaration, we set the variable $i to 1.

For the condition, we have the loop execute if $i is less than 10.

Finally, for the action, we add 1 to the value of $i for every loop iterations — that is, every time the loop code is executed.

When run, this script will count from 1 to 10, outputting text along the way.

Note that it will not actually output Number 10 because we specify that $i must be less than 10, not less than or equal to it.

FUNCTIONS IN PHPDespite the fact that php comes with such a large selection of functions to perform all sorts of tasks, you will want to create your own function when the need arises.

If you find yourself doing the same thing repeatedly, or you want to share code across projects, a user function is for you.

You can give your functions whatever name you like, they follow the same guidelines as php variables without the $ sign.

You may not redefine PHP's built-in functions names do not collide with existing PHP functions — just because you don’t have the imagepng() function available, it doesn’t mean others also won’t.

The simplest user function in PHP looks something like this:function foo(){ return 1;}print foo();You define your function with the function keyword, followed by the name of the function and two parentheses.

The actual code of your function will execute lies between braces.

After the function definition, we can treat foo() like any other function, as seen in line 5 where we print out the value it returns.

RETURN VALUESYou are allowed to return one value back from functions, and you do this by using the return statement.

You can return any variable you want, as long as it is just one variable, it can be an integer, a string, a database connection, etc.

The return keyword sets up the function return value to be whatever variable you use with it, then exist the function immediately.

You can also just use return; which means “exit without sending a value back.

” If you try to assign to a variable the return value of a function that has no return value.

Your variable will be set to NULL.

Consider this statement:function foo(){ print "In function"; return 1; print "leaving function.

";}print foo();That will output In function, followed by 1, and then the script will terminate.

The reason we never see leaving function.

is because the line return 1 passes one back then immediately exits — the second print statement if foo() is never reached.

PARAMETERSYou can design your functions to accept parameters by modifying the definition to include as many as you want.

You need to give each parameter the name you will be used to refer to it inside the function — when you later call that function, PHP will copy the values it receives into these parameters, like this:<?php function multiply($a, $b) { $total = $a * $b; return $total; }$mynum = multiply(10 , 5);?>After running the script, $mynum will be set to 50.

The multiply() function could have been rewritten so that it was just one line: return $a * $b, but it is good to show that you can make your function as long as you wantPASSING BY REFERENCEWhen it comes to reference, things get more complicated because you need to be able to accept parameters by reference and also return values by reference.

This is done with the reference operator, &.

Making a parameter as “passed by reference” is done in the function definition, not in the function call, that is:function multiply($num1, $num2) {is correct, whereas$mynum = multiply(&10, &5);this one is wrong.

This means that if you have a function being used multiple times across your project, you only need to edit the function definition to make it take variables by reference.

Passing by reference is often a good way to make your script shorter and easier to read — the choice is rarely driven by performance considerations.

RETURNING BY REFERENCEUnlike passing values by reference, where you specify the referenced nature of the parameter in the function definition, to return reference you need to specify such in the definition and at call time.

To specify that a function should return a reference, you place the ampersand reference operator before the function name, and to specify that you wish to reference the result of the function as opposed to copying it, you use the normal reference assign that you learned earlier.

Here’s how that looks:function &return_fish() { $fish = "ganda"; return $fish;}$fish_ref = & return_fish();DEFAULT PARAMETERWhen designing your function, it is often helpful to assign default values for the parameter that aren’t passed.

PHP does this for most of its functions, and it saves you having to pass in parameter most of the time if they are usually the same.

To define your own default parameters for a function, add the constant value you would like them to be set to after the variable, like this:function doHello($Name = "Paul") { return "Hello $Name.";}doHello();doHello("Paul");doHello("Andrew");output:Hello Paul!Hello Paul!Hello Andrew!Now, consider this function:function doHello($FirstName, $LastName = "Smith") {}That does not mean that both $FirstName and $LastName should be set to Smith.

Instead, only $LastName gets the value — PHP treats the two variables as functionally independent of each other, which means you can use code like this:function doHello($FirstName = "John", $LastName = "Smith") { return "Hello, $FirstName $LastName.";}So, to greet three people named John Smith, Tom Davies, and Tom Smith, you would use this code:doHello();doHello("Tom", "Davies");doHello("Tom");If you wanted to greet someone named John Wilson, ideally you would let PHP fill in the first parameter for you, as John is the default for the function, and you would provide the Wilson part.

But if you try code like this, you will see it does not work:doHello("Wilson");Instead of John Wilson, you will get Wilson Smith — PHP will assume the parameter you provided was for the first name, as it fills its parameter from left to right.

The same logic dictates that you cannot put a default value before a non-default value, like this:function doHello($FirstName = "Joe", $LastName) {}If someone used doHello(“Peter”), would they be trying to provide a value for $FirstName to use instead of the default, or do they want the default value in there and Peter for $LastName.Hopefully, you can see why PHP will flag up an error if you attempt this!.

. More details

Leave a Reply