Found 1060 Articles for PHP

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

Access variables from parent scope in anonymous PHP function

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

582 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

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

What does [Ss]* 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.

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

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

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

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

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

Advertisements