Found 1046 Articles for PHP

How do I iterate through DOM elements in PHP?

AmitDiwan
Updated on 09-Apr-2020 11:34:16

964 Views

Following is the XML data (input) −                       Iterating through the elements in the DOM object.Example$elements = $dom->getElementsByTagName('foo'); $data = array(); foreach($elements as $node){    foreach($node->childNodes as $child) {       $data[] = array($child->nodeName => $child->nodeValue);    } }OutputThis will produce the following output −Every ‘foo’ tag will be iterated over and specific ‘bar’ and ‘pub’ value will be obtained, i.e specific child nodes can be accessed by their names itself.The elements in the XML file are obtained by ... Read More

What does the '@' prefix do in PHP?

AmitDiwan
Updated on 09-Apr-2020 11:31:53

583 Views

The ‘@’ symbol suppresses errors from getting displayed on the screen.PHP supports an error control operator, i.e the sign (@). When it is prepended to an expression in PHP, error messages that could get generated when it uses that expression will be ignored.If the track_errors attribute is enabled, the error message generated by the expression will be saved in the variable named $php_errormsg. This variable will be overwritten each time on every error.It is always suggested to write code that works relevantly with error states/conditions.

Is it possible to get list of defined namespaces in PHP

AmitDiwan
Updated on 09-Apr-2020 11:31:04

464 Views

Given file 1 has namespace ns_1 and file 2 has namespace ns_2, if file 1 and file 2 are included in file 3, there is no way to know that namespaces ns_1 and ns_2 have been loaded.The only way is to use the ‘class_exists’ function and the list of classes with the specific namespace can be obtained using ‘get_declared_classes’. Simply put, this data obtained can be used to find a matching namespace given all the declared class names −function namespaceExists($namespace) {    $namespace .= "\";    foreach(get_declared_classes() as $name)    if(strpos($name, $namespace) === 0) return true;    return false; }----Or---Example Live Demo

How to open an Excel file with PHPExcel for both reading and writing?

AmitDiwan
Updated on 09-Apr-2020 11:28:33

3K+ Views

There is no concept of opening a file for read and write in PHPExcel since it is not aware of the source of the PHPExcel object. Irrespective of the source from where the file was loaded or the type of file, the file can be read based on its named and saved with the same name. This way, the file will be overwritten, and new changes will be reflected in the file.Exampleerror_reporting(E_ALL); set_time_limit(0); date_default_timezone_set('Europe/London'); set_include_path(get_include_path() . PATH_SEPARATOR . './Classes/'); include 'PHPExcel/IOFactory.php'; $fileType = 'Excel5'; $fileName = name_of_file.xls'; // Read the file $objReader = PHPExcel_IOFactory::createReader($fileType); $objPHPExcel = $objReader->load($fileName); // Change the ... Read More

PHP readfile vs. file_get_contents

AmitDiwan
Updated on 09-Apr-2020 11:26:26

736 Views

The ‘readfile’ function is an inbuilt function in PHP that reads the file directly into the output buffer. The name of the file is passed as a parameter to the function. It returns the number of bytes that were read in case of successfully reading the data. It returns FALSE otherwise −Example Live DemoOutputThis will produce the following output −Sorry, the file could not be openedThe ‘file_get_contents’ function is an inbuilt function in PHP that loads the file into memory and displays the contents only when the echo function is called. During this phase, the data is copied from memory to ... Read More

Storing objects in PHP session

AmitDiwan
Updated on 09-Apr-2020 11:25:00

4K+ Views

The serialize() function in PHP can be used before storing the object, and the unserialize() function can be called when the object needs to be retrieved from the session.The function converts a storable representation of a specific value into a sequence of bits. This is done so that the data can be stored in a file, a memory buffer or can be transferred over a network.Using the serialize function to store the object −session_start(); $object = new sample_object(); $_SESSION['sample'] = serialize($object);The session is started by using the ‘session_start’ function and a new object is created. The object created is serialized ... Read More

Generate all combinations of a specific size from a single set in PHP

AmitDiwan
Updated on 09-Apr-2020 11:22:49

1K+ Views

To generate all combinations of a specific size from a single set, the code is as follows −Example Live Demofunction sampling($chars, $size, $combinations = array()) {    # in case of first iteration, the first set of combinations is the same as the set of characters    if (empty($combinations)) {       $combinations = $chars;    }    # size 1 indicates we are done    if ($size == 1) {       return $combinations;    }    # initialise array to put new values into it    $new_combinations = array();    # loop through the existing combinations and ... Read More

Access variables from parent scope in anonymous PHP function

AmitDiwan
Updated on 09-Apr-2020 13:43:42

432 Views

The ‘use’ keyword can be used to bind variables into the specific function’s scope.Use the use keyword to bind variables into the function's scope −Example Live Demo

Will enabling XDebug on a production server make PHP slower?

AmitDiwan
Updated on 09-Apr-2020 11:18:51

404 Views

Yes, debuggers like XDebug reduce the performance of the PHP server. This is the reason why debuggers are not placed in server environment. They are deployed in a different environment to avoid unnecessary overheads.Debug messages can’t be displayed in an application that is already in the production phase.When debugging behavior is added to the server, the debug engine gets attached to the PHP process. It starts to receive messages to stop at breakpoint, but this is not the required behavior, since it introduces a high-performance blow to other processes thereby stopping the PHP parser.On the other hand, when a debugger ... Read More

What does [Ss]* mean in regex in PHP?

AmitDiwan
Updated on 09-Apr-2020 11:17:47

316 Views

The regular expression [\S\s]* in PHP means “don’t match new lines”.In PHP, the /s flag can be used to make the dot match all the characters −Example Live Demoprint(preg_match('/^[\S\s]$/s',"hello world"));OutputThis will produce the following output −0The ‘preg_match’ function is used to match regular expression with the given input string. Here since the ‘\’ is encountered, it returns 0.

Advertisements