Skip to content

Instantly share code, notes, and snippets.

@smohadjer smohadjer/php7.md
Last active Mar 7, 2020

Embed
What would you like to do?
PHP tips and tircks

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
You can’t perform that action at this time.