Found 1046 Articles for PHP

Sort multidimensional array by multiple keys in PHP

AmitDiwan
Updated on 06-Apr-2020 07:53:22

3K+ Views

The array_multisort function can be used to sort a multidimensional array based on multiple keys −Example$my_list = array(    array('ID' => 1, 'title' => 'data one', 'event_type' => 'one'),    array('ID' => 2, 'title' => 'data two', 'event_type' => 'zero'),    array('ID' => 3, 'title' => 'data three', 'event_type' => 'one'),    array('ID' => 4, 'title' => 'data four', 'event_type' => 'zero') ); # The list of sorted columns and their data can be obtained. This will be passed to the array_multisort function. $sort = array(); foreach($my_list as $k=>$v) {    $sort['title'][$k] = $v['title'];    $sort['event_type'][$k] = $v['event_type']; } # ... Read More

Convert Dashes to CamelCase in PHP

AmitDiwan
Updated on 06-Apr-2020 07:38:16

1K+ Views

Following is the code to convert dashes to CamelCase in PHP −Sample input − this-is-a-test-stringSample output − thisIsATestStringNote − There is no need to use regex or callbacks. It can be achieved using ucwords.function dashToCamelCase($string, $capitalizeFirstCharacter = false) {    $str = str_replace(' ', '', ucwords(str_replace('-', ' ', $string)));    if (!$capitalizeFirstCharacter) {       $str[0] = strtolower($str[0]);    }    return $str; } echo dashToCamelCase('this-is-a-string');For PHP version>=5.3, the below code can be used −function dashToCamelCase($string, $capitalizeFirstCharacter = false) {    $str = str_replace('-', '', ucwords($string, '-'));    if (!$capitalizeFirstCharacter) {       $str = lcfirst($str);    } ... Read More

How to upload multiple files and store them in a folder with PHP?

AmitDiwan
Updated on 06-Apr-2020 07:36:51

16K+ Views

Below are the steps to upload multiple files and store them in a folder −Input name must be defined as an array i.e. name="inputName[]"Input element should have multiple="multiple" or just multipleIn the PHP file, use the syntax "$_FILES['inputName']['param'][index]"Empty file names and paths have to be checked for since the array might contain empty strings. To resolve this, use array_filter() before count.Below is a demonstration of the code −HTMLPHP$files = array_filter($_FILES['upload']['name']); //Use something similar before processing files. // Count the number of uploaded files in array $total_count = count($_FILES['upload']['name']); // Loop through every file for( $i=0 ; $i < $total_count ; ... Read More

Extract a property from an array of objects in PHP

AmitDiwan
Updated on 06-Apr-2020 07:34:36

3K+ Views

Given the below code, the task is to extract the ID of the my_object variable −Example$my_object = Array ( [0] => stdClass Object    (       [id] => 12    ),    [1] => stdClass Object    (       [id] => 33    ),    [2] => stdClass Object    (       [id] => 59    ) )The array_map function can be used for older versions of PHP. Below is a demonstration of the same.$object_id = array_map(create_function('$o', 'return $o->id;'), $objects);For PHP version 5.5 or higher, the array_column function could be used. Below is a demonstration of the same −$object_id = array_column($my_object, 'id');OutputThis will produce the following output −[12, 33, 59]

How to identify server IP address in PHP?

AmitDiwan
Updated on 06-Apr-2020 07:31:25

3K+ Views

The server IP can be identified with the below line of code −$_SERVER['SERVER_ADDR'];The port can be identified using the below line of code −$_SERVER['SERVER_PORT'];For PHP version 5.3 and higher, the following lines of code can be used −$host_addr= gethostname(); $ip_addr = gethostbyname($host_addr);This can be used when a stand-alone script is being run (which is not running via the web server).

How to force file download with PHP?

AmitDiwan
Updated on 06-Apr-2020 07:30:32

2K+ Views

The below code can be used to force a file to get downloaded in PHP.

Can HTML be embedded inside PHP “if” statement?

AmitDiwan
Updated on 06-Apr-2020 07:27:27

5K+ Views

Yes, HTML can be embedded inside an ‘if’ statement with the help of PHP. Below are a few methods.Using the if condition −    it is displayed iff $condition is met Using the if and else if conditions −     it is displayed iff $condition is met    HTML TAG HERE    HTML TAG HERE Embedding HTML inside PHP − HTML TAG HERE

How to detect search engine bots with PHP?

AmitDiwan
Updated on 06-Apr-2020 07:21:13

1K+ Views

A search engine directory of the spider names can be used as a reference. Next, $_SERVER['HTTP_USER_AGENT']; can be used to check if the agent is a spider (bot).Below is an example demonstrating the same −if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "some_bot_name")) {    //other steps that need to be used }Code explanation − The agent, along with the user agent is passed to the strtolower function, whose output in turn is passed to the strstr function. Both the user agent and the bot are compared to see if the spider is a bot or not.Another option is shown below −function _bot_detected() {    return (   ... Read More

In PHP, can you instantiate an object and call a method on the same line?

AmitDiwan
Updated on 06-Apr-2020 07:06:10

719 Views

Yes, an object can be instantiated and a method can be called on a single line using PHP. This feature has come into effect beginning from PHP version 5.4.An object can be instantiated by accessing the class member of the class. This can be seen in the below snippet −(new my_var)-> my_instance()Code explanation − Here, my_instance is the method and my_var is the object that needs to be instantiated.Example Live Democlass Test_class {    public function __construct($param) {       $this->_var = $param;    }    public function my_method() {       return $this->_var * 2;    }   ... Read More

Print newline in PHP in single quotes

AmitDiwan
Updated on 06-Apr-2020 07:03:11

419 Views

Since can’t be used with single quotes, we need to resort to other options.When using command line interface, the constant PHP_EOL can be used.When using with browsers, the ‘’ can be used.Both the options have been demonstrated below.Suppose our option was not cli, the ‘else’ part will be executed and a newline will be printed −Example Live DemoOutputThis will produce the following output −hi hello hihello

Advertisements