Use var_dump() to echo a variable: https://www.php.net/manual/en/function.var-dump.php
The version of PHP available from the command line has nothing to do with the version of PHP loaded as a module. These are completely separate things. The version you see in browser when you run phpinfo()
is the version Apache is using and the version you see when you run which php
in cli is the version command line is using. If after upgrading to a new version of php typing php --version
in terminal still shows old version of php you need to update the php path of your $PATH variable which we do using .bash_profile. This is a hidden file in your Mac’s user directory which is loaded before Terminal loads your shell environment and contains all the startup configuration and preferences for your command line interface. Take note that Bash shell uses colon (:) as the path separator; while windows use semicolon (;).
- Find the php folder where new version of php is installed. You can find this in output of phpinfo().
- Go to your home directory
cd ~
and open your bash profile:nano .bash_profile
. - Add the new path:
export PATH="/usr/local/php5/bin:$PATH"
- Save changes and exit.
- To execute .bash_profile either close terminal and open it again or run
source ~/.bash_profile
. - Enter
php --version
to verify new version of php used by terminal.
You can use env
or echo $PATH
in terminal to see value of your PATH variable. You can edit paths directly via sudo nano /etc/paths
, but this is not recommended since your changes after all users. The better way to edit paths is using .bash_profile since then changes only apply to your user.
<?php if($condition) : ?>
html here
<?php else : ?>
even more html
<?php endif; ?>
<?php if (condition) { ?>
html here
<?php } ?>
Elvis operand in php (?:) is a shorthand ternary operator and does the same as || do in javascript, i.e. returning its first operand if it evaluates to true, otherwise evaluating and returning its second operand.
$color = getenv('SPACE_COLOR') ?: 'gold';
Use foreach
to loop arrays
foreach (array_expression as $value)
statement
foreach (array_expression as $key => $value)
statement
in_array — Checks if a value exists in an array https://www.php.net/manual/en/function.in-array
count — Count all elements in an array, or something in an object https://www.php.net/manual/en/function.count
array_key_exists() checks if the given key or index exists in an array
<?php
$array1 = array("Orange" => 100, "Apple" => 200, "Banana" => 300, "Cherry" => 400);
if (array_key_exists("Banana", $array1))
{
echo "Array Key exists...";
}
?>
Null coalescing operator
$name = $_GET['name'] ?? 'john doe';