Found 1060 Articles for PHP

Difference between the | and || or operator in php

Mahesh Parahar
Updated on 13-Jan-2020 06:35:31

369 Views

'|' Bitwise OR operator'|' operator is a bitwise OR operator and is used to set the bit to 1 if any of the corresponding bit is 1.'||' Logical Or operator'||' is a logical Or operator and works on complete operands as whole.ExampleFollowing example, shows usage of '|' vs '||' operators. Live Demo    PHP Example     Output$x | $y = 3 $x || $y = 1

Difference between !== and ==! operator in PHP

Mahesh Parahar
Updated on 13-Jan-2020 06:33:04

546 Views

'!==' comparison operator'!==' operator checks the unequality of two objects with a type check. It does not converts the datatype and makes a typed check.For example 1 !== '1' will results true.'==!' comparison operator'==!' operator is combination of two operators and can be written as == (!operands).ExampleFollowing example, shows usage of '!==' vs '==!' operators. Live Demo    PHP Example     Output$x !== operator $y = bool(true) $x ==! operator $y = bool(true)

Difference between the Ternary operator and Null coalescing operator in php

Mahesh Parahar
Updated on 06-Jan-2020 06:15:23

484 Views

Ternary operatorTernary operator is used to replace if else statements into one statement.Syntax(condition) ? expression1 : expression2;Equivalent Expressionif(condition) {    return expression1; } else {    return expression2; }If condition is true, then it returns result of expression1 else it returns result of expression2. void is not allowed in condition or expressions.Null coalescing operatorNull coalescing operator is used to provide not null value in case the variable is null.Syntax(variable) ?? expression;Equivalent Expressionif(isset(variable)) {    return variable; } else {    return expression; }If variable is null, then it returns result of expression.Example    PHP Example     Outputnot passed not passed

How to parse a CSV file using PHP

AmitDiwan
Updated on 30-Dec-2019 06:56:12

560 Views

To parse a CSV file in PHP, the code is as follows. Under fopen(), set the path of the .csv file−Example$row_count = 1; if (($infile = fopen("path to .csv file", "r")) !== FALSE) {    while (($data_in_csv = fgetcsv($infile, 800, ", ")) !== FALSE) {       $data_count = count($data_in_csv);       echo " $data_count in line $row_count: ";       $row_count++;       for ($counter=0; $counter < $data_count; $counter++) {          echo $$data_in_csv[$counter] . "";       }    }    fclose(infile); }Code explanation − The file can be opened in reading ... Read More

Performance of FOR vs FOREACH in PHP

AmitDiwan
Updated on 30-Dec-2019 06:53:00

2K+ Views

The 'foreach' is slow in comparison to the 'for' loop. The foreach copies the array over which the iteration needs to be performed.For improved performance, the concept of references needs to be used. In addition to this, ‘foreach’ is easy to use.ExampleBelow is a simple code example − Live DemoOutputThis will produce the following output −This completed in 0.00058293342590332 seconds This completed in 0.00063300132751465 seconds This completed in 0.00023412704467773 seconds This completed in 0.00026583671569824 seconds

Check whether property exists in object or class with PHP

AmitDiwan
Updated on 30-Dec-2019 06:50:33

3K+ Views

The property_exists() or the isset() function can be used to check if the property exists in the class or object.SyntaxBelow is the syntax of property_exists() function−property_exists( mixed $class , string $property )Exampleif (property_exists($object, 'a_property'))Below is the syntax of isset() function−isset( mixed $var [, mixed $... ] )Exampleif (isset($object->a_property))The isset() will return false if the ‘a_property’ is null.ExampleLet us see an example − Live DemoOutputThis will produce the following output−bool(true) bool(true)

Get all subdirectories of a given directory in PHP

AmitDiwan
Updated on 30-Dec-2019 06:45:37

2K+ Views

To get the subdirectories present in a directory, the below lines of code can be used −Example Live DemoOutputThis will produce the following output. The glob function is used to get all the subdirectories of a specific directory−Array (    [0] => demo.csv    [1] => mark.php    [2] => contact.txt    [3] => source.txt )To get only the directories, the below lines of code can be used−ExampleOutputThis will produce the following output. The glob function is used by specifying that only directories need to be extracted−Array (    [0] => example    [1] => exam    [2] => log )

PHP Regex to get YouTube Video ID

AmitDiwan
Updated on 30-Dec-2019 06:41:43

767 Views

The parse_url and parse_str functions can be used to get the ID of a specific YouTube video.Example Live DemoOutputVX96I7PO8YUIn the above code, the parse_url function takes in a string and slices it into an array of information. The element that the user specifically wants to work with can be specified as a second argument or the entire array can be used.A YouTube video has an ID that can be seen in the URL. The goal is to fetch the ID after the letter ‘v’ and before ‘&’. To accomplish this, the parse_str function can be used. It is similar to GET ... Read More

How to capture the result of var_dump to a string in PHP?

AmitDiwan
Updated on 30-Dec-2019 06:38:35

2K+ Views

The resultant value of var_dumo can be extracted to a string using ‘output buffering’. Below is an example demonstrating the same −Example Live Demo

A Preferred method to store PHP arrays (json_encode or serialize)?

AmitDiwan
Updated on 30-Dec-2019 06:36:39

465 Views

This depends on the requirements in hand.JSON is quicker in comparison to PHP serialization unless the following conditions are met−Deeply nested arrays are stored.The objects that are stored need to be unserialized to a proper class.The interaction is between old PHP versions that don't support json_decode.The below line of code can be used to store PHP arrays using json_encode−json_encode($array, JSON_UNESCAPED_UNICODE)JSON doesn't store the object's original class anywhere, but it can be restored as class instances belonging to stdClass.Why use json_encode instead of serializing?JSON is much more portable in comparison to serialize.The features of __sleep() and __wakeup() can't be leveraged using ... Read More

Advertisements