Found 1046 Articles for PHP

How do you pass objects by reference in PHP 5?

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

277 Views

A PHP reference is an alias, that allows two different variables to write it to the same value. In PHP version 5, an object variable doesn't contain the object itself as its value. It holds an object identifier that allows object accessors to find the actual object.When an object is sent by an argument, returned or assigned to a different variable, these different variables are not aliases. They contain a copy of the identifier, that points to the same object.Example$my_var = new class_name; echo $my_var->get_class_name(5)->value; $my_var->test(); echo $my_var->get_class_name(5)->value;OutputThis will produce the following output −class_name #5This isn't "pass by reference". It ... Read More

How to find PHP execution time?

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

218 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

648 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

440 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

725 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

278 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

308 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

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

660 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

499 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

Advertisements