What Does the Prefix Do in PHP

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

735 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.

Get List of Defined Namespaces in PHP

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

641 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

Open Excel File with PHPExcel for 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

1K+ 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

5K+ 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

2K+ 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

Will Enabling Xdebug on a Production Server Make PHP Slower?

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

483 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

Meaning of 'mean' in Regex in PHP

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

424 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.

Extract or Uncompress Gzip File Using PHP

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

1K+ Views

The zipped files can be unzipped or decompressed using PHP’s gzread function. Below is the code example for the same −Example$file_name = name_of/.dump.gz'; $buffer_size = 4096; // The number of bytes that needs to be read at a specific time, 4KB here $out_file_name = str_replace('.gz', '', $file_name); $file = gzopen($file_name, 'rb'); //Opening the file in binary mode $out_file = fopen($out_file_name, 'wb'); // Keep repeating until the end of the input file while (!gzeof($file)) {    fwrite($out_file, gzread($file, $buffer_size)); //Read buffer-size bytes. } fclose($out_file); //Close the files once they are done with gzclose($file);OutputThis will produce the following output −The uncompressed data ... Read More

Why PHP is Suitable for High Traffic Websites

AmitDiwan
Updated on 09-Apr-2020 11:14:23

288 Views

The main reason behind websites being slow is overloading of hosts.The benefit of using PHP over other compiled languages is the ease of maintenance. PHP has been designed ground up to efficiently handle HTTP traffic, there is less to build in comparison to building using other compiled languages. In addition to this, merging changes is an easier task since the server doesn’t need to be recompiled and restarted (which needs to be done in case of using a compiled binary- FastCGI).PHP, when properly written, can be scaled to a great extent. Other limiting factors include the database engine that is ... Read More

Advertisements