in industry php arrays are used to store data in a list. eg: $myArray = [1, 2, 3, 4, 5];
to print the array we use print_r() function. eg: print_r($myArray);
we can change the value of an array at index 2 to a new value. eg: $myArray[2] = "hello";
associative arrays
associative arrays are arrays that have keys and values. eg: $myArray = ["key" => "value"];
array_key_exists(): checks if an array has a key. eg: array_key_exists("key", $myArray);
adding a new key to an array. eg: $myArray["key"] = "value";
overwriting a key in an array. eg: $myArray["key"] = "value";
multi dimension array
multi dimension arrays are arrays that have arrays inside them. eg: $myArray = [["key" => "value"], ["key" => "value"]];
indexed multi dimension arrays are arrays that have indexed arrays inside them. eg: $myArray = [[0, 1, 2], [0, 1, 2]];
Array functions
count(): returns the number of elements in an array. eg: echo count($myArray);
array_key_exists(): returns true if the given key exists in the array. eg: echo array_key_exists(0, $myArray);
array_keys(): returns an array containing the keys of the array. eg: echo array_keys($myArray);
array_values(): returns an array containing the values of the array. eg: echo array_values($myArray);
array_merge(): merges two or more arrays. eg: $myArray = array_merge($myArray, $myOtherArray);
array_pop(): removes the last element of an array. eg: array_pop($myArray);
array_shift(): removes the first element of an array. eg: array_shift($myArray);
array_unshift(): adds an element to the beginning of an array. eg: array_unshift($myArray, 10);
array_push(): adds an element to the end of an array. eg: `array_push($myArray, 10)
Include and Require
code once and use it multiple times
include: includes a file. eg: include "myFile.php";
include will stop the script if the file is not found, it will not throw an error. it will carry on with rest of the script.
require: includes a file. eg: require "myFile.php";
require will stop the script if the file is not found, it will throw an error. it will not carry on with rest of the script.
include_once: includes a file only once if it is not already included. eg: include_once "myFile.php";
Control Structures and Loops
Loops in php
loops are used to execute a block of code a number of times. eg:
when we want to execute a block of code 5 times for ($i = 0; $i < 5; $i++) {}
Imagine if we have a list of names and we want to print them all. we can use a loop to do this. a for loop in php for ($i = 0; $i < count($myArray); $i++) {echo $myArray[$i];}}
foreach -> loops through an array. eg: foreach ($myArray as $value) {echo $value;}
while -> loops while a condition is true. eg: while ($i < 10) {echo $i; $i++;}
// for loopfor ($i = 0; $i < 5; $i++) {
echo$i;
}
//for each loopforeach ($myArrayas$value) {
echo$value;
}
// while loopwhile ($i < 10) {
echo$i;
$i++;
}
// do while loopdo {
echo$i;
$i++;
} while ($i < 10);
boolean data type is used to store true or false values. eg: $myBoolean = true;
this is because the values are converts into strings. eg: echo true; will give 1 and echo false; wont output anything
some comparison operation that return boolean
==: checks if two values are equal. eg: if ($myBoolean == true) {echo "true";}
===: checks if two values are equal and of the same type. eg: if ($myBoolean === true) {echo "true";}
!=: checks if two values are not equal. eg: if ($myBoolean != true) {echo "true";}
!==: checks if two values are not equal and of the same type. eg: if ($myBoolean !== true) {echo "true";}
>: checks if the first value is greater than the second value. eg: if ($myBoolean > true) {echo "true";}
<: checks if the first value is less than the second value. eg: if ($myBoolean < true) {echo "true";}
>=: checks if the first value is greater than or equal to the second value. eg: if ($myBoolean >= true) {echo "true";}
!: negates a boolean value. eg: if (!$myBoolean) {echo "true";}
&&: checks if both values are true. eg: if ($myBoolean && true) {echo "true";}
||: checks if either value is true. eg: if ($myBoolean || true) {echo "true";}
conditional statements using if
a major part of programming is conditional statements. eg: if ($myBoolean) {echo "true";}
example of conditional statement in real life is if you are driving and you are not allowed to drive. eg: if ($myAge>18) {echo "true";} else {echo "false";}
example of nested conditional in real life is if only people with age above 18 and below 25 can drive. eg: if ($myAge>18) {if ($myAge<25) {echo "true";} else {echo "false";}} else {echo "false";}
using &&if ($myAge>18 && $myAge<25) {echo "true";} else {echo "false";}
using ||if ($myAge>18 || $myAge<25) {echo "true";} else {echo "false";}
break and continue
break: breaks out of the loop. eg: for ($i = 0; $i < 5; $i++) {if ($i == 3) {break;}} this will stop the loop at the 3rd iteration.
continue: continues to the next iteration of the loop. eg: for ($i = 0; $i < 5; $i++) {if ($i == 3) {continue;}} this will skip the 3rd iteration.
functions
functions are just block of code that can be used to perform a specific task and can be called anywhere eg: function myFunction() {echo "hello";}
something to note is that functions are not hold in memory. they are just a block of code that can be called. eg: myFunction();
we can pass arguments to functions. eg: function myFunction($myArgument) {echo $myArgument;}
scope refers to the visibility of variables. eg: $myVariable is global and can be used anywhere in the script.
a variable can be local to a function or global to the script. eg of local variable: function myFunction() {$myVariable = "hello";} this variable cannot be used outside of the function.