Found 26504 Articles for Server Side Programming

Will enabling XDebug on a production server make PHP slower?

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

485 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

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

How can I 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 is PHP apt for high-traffic websites?

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

292 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

Is there a PHP function that only adds slashes to double quotes NOT single quotes

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

624 Views

The json_encode function can be used to add slashes to double quotes. Also ‘addcslahses’ can also be used to add ‘\’ to specific characters −Example Live DemoOutputThis will produce the following output −Hello \there!The ‘addcslashes’ function is used to return a string with backslashes in front of the specific characters. It is a case sensitive function and should usually not be used with 0 (null), r (carriage return), n (newline), f (form read), t (tab), v (vertical tab) values. This is because values like \0, \r, , \t, \f and \v are pre-define escape sequences.In the above code, the ‘addcslashes’ function ... Read More

How to redirect domain according to country IP address in PHP?

AmitDiwan
Updated on 09-Apr-2020 11:12:10

2K+ Views

The GeoIP extension can be used to find the exact location of an IP address. Apart from this, the geoPlugin class can be downloaded from −https://www.geoplugin.com/_media/webservices/geoplugin.class.phpsThe country code list can be found in the below link −https://www.geoplugin.com/iso3166An index.php file can be placed inside the root folder and the below lines of code can be put inside this index file −Once the geoplugin class has been downloaded, a new instance is created and given the name ‘geoplugin’. The locate function is called on this instance of the geoplugin class. The same class object’s countryCode is assigned to a variable named ‘var_country_code’. ... Read More

PHP: Remove object from array

AmitDiwan
Updated on 09-Apr-2020 11:10:50

4K+ Views

The unset function can be used to remove array object from a specific index in PHP −Example Live Demo$index = 2; $objectarray = array(    0 => array('label' => 'abc', 'value' => 'n23'),    1 => array('label' => 'def', 'value' => '2n13'),    2 => array('label' => 'abcdef', 'value' => 'n214'),    3 => array('label' => 'defabc', 'value' => '03n2') ); var_dump($objectarray); foreach ($objectarray as $key => $object) {    if ($key == $index) {       unset($objectarray[$index]);    } } var_dump($objectarray);OutputThis will produce the following output −array(4) { [0]=> array(2) { ["label"]=> string(3) "abc" ["value"]=> string(3) "n23" } [1]=> ... Read More

PHP: How do I display the contents of a textfile on my page?

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

3K+ Views

The file_get_contents function takes the name of the php file and reads the contents of the text file and displays it on the console. get the contents, and echo it out.The contents of filename.php would be the output.In the above code, the function ‘file_get_contents’ is called by passing the php file name. The output will be the contents present in the php file.

How to display XML in HTML in PHP?

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

2K+ Views

If the string is pre-formatted, and a plain text representation of the same is needed, it can be wrapped in a HTML tag and html entities can be used to escape the angle brackets. This has been shown below −The string is assigned to a string type and the above is used to show XML in HTML −Example Live DemoOutputThis will produce the following output −           EXAMPLE    

Remove duplicated elements of associative array in PHP

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

2K+ Views

The ‘array_map’ function sends the value of every element in the array to a user-defined function. It then returns an array with new values, because of calling the user-defined function on the array.Syntax of array_map functionarray_map ( user-defined function, array_1, array_2, array_3…)The user-defined function and the array_1 are compulsory arguments, but array_2 and array_3 are optional.Example Live Demo$result = array(    0=>array('a'=>1, 'b'=>'Hello'),    1=>array('a'=>1, 'b'=>'duplicate_val'),    2=>array('a'=>1, 'b'=>'duplicate_val') ); $unique = array_map("unserialize", array_unique(array_map("serialize", $result))); print_r($unique);OutputThis will produce the following output −Array ( [0] => Array ( [a] => 1 [b] => Hello ) [1] => Array ( [a] => 1 ... Read More

Advertisements