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
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.
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
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
ID has to be unique in a document −var option_avail = $('input[name=option_avail]:checked', '#controls').val() firstoption secondoption thirdoption
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)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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
JShell is the REPL tool that has introduced in Java 9. We can use this tool to execute simple snippets in the command-line prompt.When we enter an arithmetic expression, variable, etc in JShell, then it displays the result without details of the type of variable created. It is possible in JShell to display more information about the execution of an entered command, use verbose mode. We need to obtain more information on the commands executed by using the command: "/set feedback verbose" (the command can be preceded by "/").In the below snippet, the verbose mode is on, and it can able to ... Read More
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
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
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