Server Side Programming Articles - Page 1841 of 2650

How to find PHP execution time?

AmitDiwan
Updated on 06-Apr-2020 09:18:38

321 Views

In PHP version 7+, the getrusage function can be used. Below is a sample code demonstration −Example Live Demo//beginning of the script $exec_start = getrusage(); //other code functionalities //end of the script function rutime($ru, $rus, $index) {    return ($ru["ru_$index.tv_sec"]*1000 + intval($ru["ru_$index.tv_usec"]/1000))    - ($rus["ru_$index.tv_sec"]*1000 + intval($rus["ru_$index.tv_usec"]/1000)); } $ru = getrusage(); echo "The process used " . rutime($ru, $exec_start, "utime") .    " ms for the computations"; echo "Spent " . rutime($ru, $exec_start, "stime") .    " ms during system calls";Note − There is no need to calculate the time difference if a php instance is spawned for every test.OutputThis will ... Read More

What is faster: many ifs, or else if in PHP?

AmitDiwan
Updated on 06-Apr-2020 09:14:44

826 Views

An else if is a better option.Below is a sample code for multiple if statements −if(condition_A){    //perform some action } if(condition_B){    //perform some action }Below is a sample code for else if statement −if(condition_A){    //perform some action } else if(condition_B){    //perform some action }When else if statements are used, if a condition is satisfied, the checking stops there and the operations associated with the condition are executed. This way, the operations and conditions get over quickly.

In php, is 123==0123?

AmitDiwan
Updated on 06-Apr-2020 09:13:40

602 Views

The answer is No. This is because 0123 means 123 with base 8 (an octal number) and its equivalent in decimal is 83.Prefixing a number with 0 indicates that it is an octal (base 8) number. This is similar to the fact that 0x indicates hex (base 16) numbers.Consider the below lines of code −Example Live Demovar_dump(123); var_dump(0123);OutputThis will produce the following output −int 123 int 83This is due to the fact that 0123 is an octal notation (notice the 0 at the beginning), whereas 123 is a decimal number.Now consider the below code −Examplevar_dump(79); var_dump(079);OutputThis will produce the following output ... Read More

Stripping last comma from a foreach loop in PHP?

AmitDiwan
Updated on 06-Apr-2020 09:09:58

900 Views

Below is the code that can be used to strip the last comma from the foreach loop −Example Live Demo$result_str = array("Hi", "Hello", "have a", "good day"); foreach ($results as $result) {    $result_str[] = $result->name; } echo implode(",",$result_str);OutputThis will produce the following output −Hi,Hello,have a,good day

How can I send radio button value in PHP using JavaScript?

AmitDiwan
Updated on 06-Apr-2020 09:08:24

369 Views

ID has to be unique in a document −var option_avail = $('input[name=option_avail]:checked', '#controls').val()     firstoption     secondoption     thirdoption

Split string into sentences using regex in PHP

AmitDiwan
Updated on 06-Apr-2020 09:07:19

454 Views

Example Live Demofunction sentence_split($text) {    $before_regexes =       array('/(?:(?:[\'\"„][\.!?…][\'\"”]\s)|(?:[^\.]\s[A-Z]\.\s)|(?:\b(?:St|Gen|Hon|Prof|Dr|Mr|Ms|Mrs|[JS]r|Col|Maj|Brig|Sgt|Capt|Cmnd|Sen|Rev|Rep|Revd)       \.\s)|(?:\b(?:St|Gen|Hon|Prof|Dr|Mr|Ms|Mrs|[JS]r|Col|Maj|Brig|Sgt|Capt|Cmnd|Sen|Rev|Rep|Revd)\.\s[A-Z]\.\s)|(?:\bApr\.\s)|(?:\bAug\.\s)|(?:\bBros\.       \s)|(?:\bCo\.\s)|(?:\bCorp\.\s)|(?:\bDec\.\s)|(?:\bDist\.\s)|(?:\bFeb\.\s)|(?:\bInc\.\s)|(?:\bJan\.\s)|(?:\bJul\.\s)|(?:\bJun\.\s)|(?:\bMar\.\s)|(?       :\bNov\.\s)|(?:\bOct\.\s)|(?:\bPh\.?D\.\s)|(?:\bSept?\.\s)|(?:\b\p{Lu}\.\p{Lu}\.\s)|(?:\b\p{Lu}\.\s\p{Lu}\.\s)|(?:\bcf\.\s)|(?:\be\.g\.\s)|(?:\besp       \.\s)|(?:\bet\b\s\bal\.\s)|(?:\bvs\.\s)|(?:\p{Ps}[!?]+\p{Pe} ))\Z/su',    '/(?:(?:[\.\s]\p{L}{1, 2}\.\s))\Z/su',    '/(?:(?:[\[\(]*\.\.\.[\]\)]* ))\Z/su',       '/(?:(?:\b(?:pp|[Vv]iz|i\.?\s*e|[Vvol]|[Rr]col|maj|Lt|[Ff]ig|[Ff]igs|[Vv]iz|[Vv]ols|[Aa]pprox|[Ii]ncl|Pres|[Dd]ept|min|max|[Gg]ovt|lb|ft|c\.?\s       *f|vs)\.\s))\Z/su',    '/(?:(?:\b[Ee]tc\.\s))\Z/su',    '/(?:(?:[\.!?…]+\p{Pe} )|(?:[\[\(]*…[\]\)]* ))\Z/su',    '/(?:(?:\b\p{L}\.))\Z/su',    '/(?:(?:\b\p{L}\.\s))\Z/su',    '/(?:(?:\b[Ff]igs?\.\s)|(?:\b[nN]o\.\s))\Z/su',    '/(?:(?:[\"”\']\s*))\Z/su',    '/(?:(?:[\.!?…] [\x{00BB}\x{2019}\x{201D}\x{203A}\"\'\p{Pe}\x{0002}]*\s)|(?:\r?))\Z/su',    '/(?:(?:[\.!?…] [\'\"\x{00BB}\x{2019}\x{201D}\x{203A}\p{Pe}\x{0002}]*))\Z/su',    '/(?:(?:\s\p{L}[\.!?…]\s))\Z/su');    $after_regexes = array('/\A(?:)/su',    '/\A(?:[\p{N}\p{Ll}])/su',    '/\A(?:[^\p{Lu}])/su',    '/\A(?:[^\p{Lu}]|I)/su',    '/\A(?:[^p{Lu}])/su',    '/\A(?:\p{Ll})/su',    '/\A(?:\p{L}\.)/su',    '/\A(?:\p{L}\.\s)/su',    '/\A(?:\p{N})/su',    '/\A(?:\s*\p{Ll})/su',    '/\A(?:)/su',    '/\A(?:\p{Lu}[^\p{Lu}])/su',    '/\A(?:\p{Lu}\p{Ll})/su'); $is_sentence_boundary = array(false, false, false, false, false, false, ... Read More

How to upload large files above 500MB in PHP?

AmitDiwan
Updated on 06-Apr-2020 09:01:55

12K+ Views

Large files can be uploaded using PHP in two ways. Both of them are discussed below −By changing the upload_max_filesize limit in the php.ini file.By implementing file chunk upload, that splits the upload into smaller pieces an assembling these pieces when the upload is completed.The php.ini file can be updated as shown below −upload_max_filesize = 50M post_max_size = 50M max_input_time = 300 max_execution_time = 300This should be avoided since it would change the settings of the server and other projects too.Updating the htacess filephp_value upload_max_filesize 50M php_value post_max_size 50M php_value max_input_time 300 php_value max_execution_time 300Changing the inline setting −ChunkingIn this ... Read More

PHP string cast vs strval function, which one should I use?

AmitDiwan
Updated on 06-Apr-2020 14:35:36

967 Views

A value can be converted into a string with the help of (string) cast or the strval() function.The strval() function is a function call whereas (string) cast is an internal type casting method.Unless there is some specific dataset or use case, both of these can be used interchangeably.This is because PHP uses automatic type conversion, due to which a variable's type is determined based on the context in which it is used.The strval($var) function returns the string value of $var whereas the (string)$var explicitly converts the "type" of $var during the process of evaluation.The $var can be any scalar type ... Read More

How to remove a function at runtime in PHP?

AmitDiwan
Updated on 06-Apr-2020 08:51:02

655 Views

The functions and classes in PHP have global scope. This means they could be called outside a function even after they were defined inside the scope and the other way round.But PHP doesn't support function overloading, and it is not possible to redefine previously-declared functions.The function can be defined as an anonymous function and it can be unset after it completes its run.Below is a code sample for the same −if (function_exists('get_magic_quotes_gpc') && @get_magic_quotes_gpc())    $my_fn = create_function('&$v, $k', '$v = stripslashes($v);');    array_walk_recursive(array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST), $my_fn);    unset($my_fn); }An anonymous function can't be called inside itself. The workaround ... Read More

Upload file with php to another php server

AmitDiwan
Updated on 06-Apr-2020 08:49:40

4K+ Views

The fopen, fread and fwrite functions can be used to open a file stream, read a data stream and write that data to a file respectively.The file resource doesn't necessarily need to point to a location on the local machine itself.Below is an example that transfers a file from the local server to ftp server −$file = "file_name.jpg"; $destination = fopen("ftp://username:password@example.com/" . $file, "wb"); $source = file_get_contents($file); fwrite($destination, $source, strlen($source)); fclose($destination);The image needs to be transferred to an FTP server. Hence the server is opened in write mode, and the image is written to that location and the stream is ... Read More

Advertisements