Functions and Objects in PHP
Functions
Functions are like variables for lines of codes.
- return a variable
- print something out.
// function being treated as variable
function is_bigger(){
return 10>=5;
}
if (is_bigger()){
echo " "
} else {
}
// passing arguments to functions
function is_bigger($a, $b){
return $a>=$b;
}
$bigger = is_bigger(10, 5)
finding built in functions
open https://www.php.net/ and use the search box to find built in functions
Try to find with help of IDE
very comprehensive resource is at https://www.php.net/funcref
Try to find the usefull functions
// built in functions
echo date('F d, Y');
// Race Car and wow is palindrome
function is_palindrom($string){
// remove spaces and converting to lower case.
$string = str_replace(' ', '', strtolower($string));
// check string by same string reversed
return ( $string == strrev( $string ));
}
$check_string = 'wow';
if (is_palindrom($check_string)){
echo "<p> $check_string is a palindrom</p>"
}
//passing variables by value means copying the value of variable and passing it to the fucntion, orignal value of variables don't change.
// we can no use variables in to the function untill unless we pass them as parameters before using them into the functions.
// call by reference
function triple(&$a){
$a *= 3 ;
}
$a = 5;
triple($a);
echo $a; // value of orignal variable is changed
$b = 10;
$c = & $b;
$c = 7;
echo $b;
//modify the array()
$numbers = array(1, 2, 3);
foreach($numbers as &$num){
triple($num);
}
print_r($numbers);
function quad(){
// There is super array called $Globals['a'] that is availablt to all php programs.
global $a;
$a *= 4;
}
Anonymous Functions
anonymous functions are created to use as callback functions. callback function are those functions which are passed as parameters in functions and do partial work of the function. Functions that don’t get a name and are created at the runtime to perform a specific task. Also called “closures” or “callback functions”.
anonymous are used in very specific cases. like sorting an obects.
// <=> spaceship operator will do greater than, less than and equal to all comparisons at once.
// example usort
$names = array('Joe', 'Erin', 'Teresa', 'Louis');
usort( $names, function($a, $b){
return $a[1] <=> $b[1]; // when second character in a will be greater than or equal to b it will return a, but when second charachter in b will be greater than a it will return b.
});
<pre>
<?php print_r($names); ?>
</pre>
Classes
A way of grouping variables and functions together as a single unit that can be referenced. we can create as many classes as we want with different names.
Objects
Instances of class we can reference single set of variables and function with the help of objects. Ojbects from the same class will have same variable names and functions but they will have different values.
Variables in classes are called “properties.”
Functions in classes are called “methods.”
Function that create the object called constructors.
To Access Properties or methods that are besed in classes we use arrow operators ->
;
// create an object
$alice = new Person('Alice');
// create 2nd object
$bob = new Person('Bob');
//Get Name
echo $alice->get_name;
//Set age
$alice->set_age(32);
class Person {
var $name;
var $age;
var $birthday = false;
function ___construct($name, $age){
$this->name = $name;
$this->age = $age;
}
public function get_name(){
return $this->name;
}
public function get_age(){
return $this->age;
}
public function set_name($new_name){
return $this->name = $new_name;
}
public function set_birthday($b){
$this->birthday = $b;
$this->update_age();
}
private function update_age(){
$this->age = ($this->birthday) ? ++$this->age :
} $this->age;
}