- BASICS
- Quotes
- Constants
- Control Structures
- Reference
- Number Systems
- VARIABLES
- Definition
- Variable Variable
- Exists
- Type Casting
- OPERATORS
- Aritmetic
- Bitwise
- String
- Comparison
- Logical
- FUNCTION
- Definition
- Anonymous
- Reference
- Variable Arguments
- ARRAY
- Basics
- Operations
- Create
-
Search
- Modify
- Sort
- Storage
- STRING
- String Basics
- String Compare
- String Search
- String Replace
- String Format
- String Regexp
- String Parse
- Formating
- Json
- STREAMS
- File Open
- Read File
- Read Csv
- File Contents
- Context
- Ob_start
- OOP
- Object Instantiation
- Class Constructor
- Interfaces, Abstract
- Resource Visibility
- Class Constants
- Namespaces
- HTTP
- Headers
- File Uploads
- Cookies
- Sessions
Value Exists
Check if a value exists in an array.
/**
* Check if value exists in arrray with isset
*/
$A = [1, 2];
$B = ['a'=>1, 'b'=>NULL];
var_dump( in_array(1, $A) ); // true
var_dump( isset($B['b']) ); // false
var_dump( isset(['a'=>1]['b']) ); // false
Array Search
Array search may return false, always use indentical operator.
/**
* array_search()
*
* The search may return false
* Always use identical operator
*/
$A = [1,2,3,4];
echo (array_search(4, $A)); // 3
echo (array_search(5, $A) == 0) == true; // 1 - Incorrect
echo (array_search(5, $A) === 0) === false; // 1 - Correct
Questions and answers
How do you check if a value exists in an array?
- a) in_array($needle, $array);
- b) in_array($array, $needle);
How do you check if a key exists in an array?
- a) in_array_key($array, $key);
- b) array_key_exists($key, $array);
How do you search the key for a given value? [a,2]
- a) array_search($val, $array);
- b) array_search($array, $key);