Skip to content

Instantly share code, notes, and snippets.

@smohadjer
Last active August 16, 2021 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smohadjer/188f0813f43b3f120f55a1753e4dd989 to your computer and use it in GitHub Desktop.
Save smohadjer/188f0813f43b3f120f55a1753e4dd989 to your computer and use it in GitHub Desktop.
PHP

Use var_dump() to echo a variable: https://www.php.net/manual/en/function.var-dump.php

Updating PHP version of terminal using .bash_profile

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 (;).

  1. Find the php folder where new version of php is installed. You can find this in output of phpinfo().
  2. Go to your home directory cd ~ and open your bash profile: nano .bash_profile.
  3. Add the new path: export PATH="/usr/local/php5/bin:$PATH"
  4. Save changes and exit.
  5. To execute .bash_profile either close terminal and open it again or run source ~/.bash_profile.
  6. 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.


Writng HTML inside a php if. The HTML will only be displayed if the condition is satisfied.

<?php if($condition) : ?>
    html here
<?php else : ?>
    even more html
<?php endif; ?>

<?php if (condition) { ?>
    html here
<?php } ?>

Elvis operand

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';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment