Loops 4. MySQL and data-rich websites 5. Security and Good Practice 6. Working with JSON, XML and APIs 7. Object-Oriented PHP Development 8. Conclusion
snippets which perform actions. • Defined using the function keyword, followed by the function name: • Function code should be wrapped in curly braces ({}), and can accept a number of parameters, which are passed into the function after the call: • function my_function( $var1, $param2, … ) { // Your function code here. // This function is called my_function. }
cannot redefine functions or use system function names. • This will trigger a parse error: • function strtolower() { // Some function code. } • We can check if a function name is already defined using the function_exists(‘strtolower’) function. • We pass the name of the function as a string • If the function name already exists, returns true. Otherwise, returns false.
• We can echo out things within a function, but it’s much better to return them instead! • function get_name( $name ) { echo $name; } • function get_name( $name ) { return $name; } echo get_name( ‘Ben’ );
Things functions CAN access: • Other functions • Function parameters • Variables defined as global • Things functions CANNOT access: • Variables defined outside of the function (unless we declare them as global).
my_function( $param1 ) { echo $myVar; // Won’t output anything because $myVar is out of scope echo $param1; // Will output OK, because $param1 is a parameter. echo strtolower( $param1 ); // Outputs OK. global $myVar; echo $myVar; // Now outputs, because $myVar is a global var. }
giving them a value in the function definition. • If we don’t assign a value to the parameter, it will be required: • function my_function( $param1 = ‘string’ ) { } my_function(); // Valid function call, $param1 assumes // ‘string’ my_function( ‘another’ ); // Also valid, $param1 is override // with ‘another’ • function my_function( $param1 ) { } my_function(); // Throws error, empty parameter. my_function( ‘string’ ); // Valid -‐ $param1 = ‘string’
function, it creates a duplicate, locally-scoped variable. • Sometimes, we want the function to modify the original variable that we passed as a parameter. • We can achieve this using pass by reference. • When we pass a variable to a function by reference, we are passing a pointer to the original variable, and it will be modified. • To define a by reference variable, we append the & literal in the parameter list.
to check the value of something, and execute code based on its state. • If the statement returns true, the code is executed. If it is false, it is not. • elseif() allows us to specify what should happen if the initial if() returns false, but we need to check a second condition. These may be used ad infinitum. • else will be executed if all of the above statements have returned false.
if( $bool1 == true ) { // Won’t execute, because $bool1 == false. } else if( $bool2 ) { // Will execute, because $bool2 == false. } else { // Will never execute, because the elseif has executed. }
for completeness. • Allows us to assign a value to a variable based on a condition (or several conditions). • Weird syntax, but remember it: • $foo = ($bar == 1) ? ‘bar is 1’ : ‘bar is not 1’; • if( $bar == 1 ) { $foo = ‘bar is 1’; } else { $foo = ‘bar is not 1’; }
for multiple values. • Much cleaner than than writing if() elseif() elseif() else blocks. • Each possibility exists as a case. • We can also define a default case, to be used when no other matches were found. • Cases can be combined to use the same code in two or more cases.
$myVar ) { case ‘some string’: // Some code in here. break; case ‘another value’: // Some other code in here. break; case ‘final value’: default: // Some more code. break; }
x iterations of something. • If we wanted to run something 10 times, for example: • for( $i = 0; $i < 10; $i++ ) { // $i will increment by 1 each time. // Any code we put in here will be executed each // iteration. } • We don’t have to use $i - it’s just short and clean; the incremental variable can be called anything (that isn’t already defined).
we need to use a different iteration variable: • for( $i = 0; $i < 10; $i++ ) { for( $j = 0; $j < 10; $j++ ) { } } • After the above has run, $i == 10 and $j == 10! • We can reset them using $i = 0;
arrays very easily. • Can be used with 0-indexed and associative arrays. • Useful when we don’t know the length of an array, or if the array has distinct keys.
‘foo’ => ‘bar’, ’hello’ => ‘world’, ‘another’ => ‘value’ ); foreach( $myArray as $key => $value ) { // $key will hold the array key for this iteration // $value will hold the value for this iteration }
while a certain condition is truthy. • Useful for looping through when we’re changing a value dynamically. • Very useful when working with databases and looping over object properties (more later on this).
execute code while a certain condition is truthy, executing it at least once. • It will execute the code defined before assessing the condition’s state. • Useful if your loop needs to execute at least once, regardless of the condition’s state. • for example, incrementing a user’s age by 1 every year until they’re 100).
// This code is executed once. // Then, $myFlag is evaluated. If it’s true, the code // executes again, and keeps going until $myFlag == false. } while( $myFlag );
month names in English 2. Write a function which returns a <select> element of month names, with the following options: • We can pass the following attributes as parameters to the function, and they are respected in the generated markup: id, class, name • The <option> elements display the month’s name, but their value is equal to its numerical index (i.e. January = 1, February = 2, etc.)