PHP Tutorials and Examples

PHP – split string examples

Splitting a string/text at a delimiter is very common use case in php. One can either use php explode (split on a fixed delimiter) or read more

PHP – take last n characters of a string

Take last 3 characters when string is big enough Take last 3 characters when string is smaller than 3 chars

PHP check if string contains something

Checking if a string contains a substring is very frequently occuring use case in PHP code. PHP functions strpos or stripos (case insensitive version) can read more

PHP – associative array value in double quoted string

To print associative array value in double quoted string in php, the following syntax using curly braces can be used. {$arr[‘key1’]} Note that curly braces read more

PHP – check if a value is in array

Checking if a value exists in array is frequently used in php. PHP in_array() can be used for this check. By default it does not read more

PHP – convert array to associative array

Convert using array_flip() This works if we need index as value. Using foreach iterator In case we need to set associative array value using array read more

PHP – print array in one line

At times for large array values we want to log them in one line for better readability. Logging an array in one line is also read more

PHP array foreach – code snippets

PHP array – foreach loop on values PHP array – foreach loop on index,values PHP array – foreach loop for modification PHP associative array – read more

PHP array map example

PHP array map applies the callback function to one or more arrays. In callback we can use a function or anonymous function. Here are some read more

PHP check if key exists in array

Checking if a key exists in an array and possibly has a non empty value are frequently used when writing php code. Accessing value of read more

PHP sort associative array using custom compare function

PHP associative array can be sorted using custom user compare function while maintaining index association using uasort. The function is passed two values and it read more

php get array value with default

Code snippet to get a value from php array and return a default value it key does not exist <?php function get_value($arr, $key, $default=null) { read more

PHP – assign multi lines string to a variable

PHP supports Heredoc and Newdoc syntax. It is a convenient way to assign multiple lines string to a variable. This makes code more readable. Here read more

PHP – call a function with arguments in array examples

PHP call_user_func_array() can be used to call a function with arguments in an array. This gives us the flexibility to pass variable number of arguments. read more

PHP – get calling file name and line number using debug_backtrace

Sometime it is useful to log the calling filename and line number details of the calling file/function in php. One common use case if to read more

PHP – get class name and file name from an object

PHP – get class name of an object get_class with object as arguments returns its class name. PHP – get filename from class name Here read more

PHP – get function arguments examples

PHP func_get_args(), func_get_arg() and func_num_args() can be used to retrieve php function arguments inside a function. This may be useful in debugging. Here are code read more

PHP – how to catch errors using set_error_handler

PHP set_error_handler can catch various type of php errors in a specific portion of the code. To restore the previous error hander one can call read more

PHP – how to initialize static variables

Sometimes we need to populate static variables in php (e.g. populating associative arrays from existing arrays). Easiest way is to define a static function (e.g. read more

PHP – how to use function static variable as cache

PHP function static variable exists only in a local function scope but it does not lose its value after function execution. It can be used read more

PHP check if a filename is valid

Sometimes we need to check if a filename contains special character etc. One possible approach is to check for alphanumeric characters and few more characters read more

PHP elapsed time

Find elapsed time in php using microtime.

PHP execute command and capture shell exit status

PHP exec and shell_exec can be used to execute a shell command on server side. exec also populates shell exit return status in one of read more

PHP how to get current url

PHP $_SERVER is an array containing information such as headers, paths, and script locations. To get current url we can use $_SERVER[‘REQUEST_URI’] and $_SERVER[‘HTTP_HOST’]. Here read more

PHP print number to two decimal places

Print a number/float in php to two decimal places:

PHP remove trailing whitespaces and newline

PHP has inbuilt function rtrim to remove specific or all trailing whitespaces from end of php string. preg_replace can also be used for this purpose. read more

Php equal (==) vs identical (===)

Equal (==) compares two values after type changing one value to another. Identical compare values and types both and returns true only if they both read more

Running php eval on code with tags

php eval on code wrapped in php tags Here is the code snippet which calls php eval on a code which is wrapped in <?php read more