Found 1060 Articles for PHP

Create nested JSON object in PHP?

AmitDiwan
Updated on 07-Apr-2020 11:01:47

2K+ Views

JSON structure can be created with the below code −$json = json_encode(array(    "client" => array(       "build" => "1.0",       "name" => "xxxx",       "version" => "1.0"    ),    "protocolVersion" => 4,    "data" => array(       "distributorId" => "xxxx",       "distributorPin" => "xxxx",       "locale" => "en-US"    ) ));

Add PHP variable inside echo statement as href link address?

AmitDiwan
Updated on 07-Apr-2020 10:59:20

4K+ Views

HTML in PHPecho "Link";orecho "Link";PHP in HTML

How do I sort a multidimensional array by one of the fields of the inner array in PHP?

AmitDiwan
Updated on 07-Apr-2020 10:58:27

253 Views

The usort function can be used to sort a multidimensional array. It sorts with the help of a user defined function.Below is a sample code demonstration −Examplefunction compare_array($var_1, $var_2) {    if ($var_1["price"] == $var_2["price"]) {       return 0;    }    return ($var_1["price"] < $var_2["price"]) ? -1 : 1; } usort($my_Array,"compare_array") $var_1 = 2 $var_2 = 0OutputThis will produce the following output −1Explanation − We have declared var_1 and var)2 with integer values. They are compared and the result is returned.

Convert all types of smart quotes with PHP

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

825 Views

The below lines of code can be used, wherein UTF-8 input is expected.$chr_map = array(    // Windows codepage 1252    "\xC2\x82" => "'", // U+0082⇒U+201A single low-9 quotation mark    "\xC2\x84" => '"', // U+0084⇒U+201E double low-9 quotation mark    "\xC2\x8B" => "'", // U+008B⇒U+2039 single left-pointing angle quotation mark    "\xC2\x91" => "'", // U+0091⇒U+2018 left single quotation mark    "\xC2\x92" => "'", // U+0092⇒U+2019 right single quotation mark    "\xC2\x93" => '"', // U+0093⇒U+201C left double quotation mark    "\xC2\x94" => '"', // U+0094⇒U+201D right double quotation mark    "\xC2\x9B" => "'", // U+009B⇒U+203A single right-pointing angle ... Read More

How do you pass objects by reference in PHP 5?

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

388 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

308 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

812 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

586 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

893 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

355 Views

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

Advertisements