AmitDiwan has Published 10744 Articles

Convert Dashes to CamelCase in PHP

AmitDiwan

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)));   ... Read More

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

AmitDiwan

AmitDiwan

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

17K+ 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 ... Read More

Extract a property from an array of objects in PHP

AmitDiwan

AmitDiwan

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

4K+ 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] ... Read More

How to identify server IP address in PHP?

AmitDiwan

AmitDiwan

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

4K+ 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 ... Read More

How to force file download with PHP?

AmitDiwan

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

AmitDiwan

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

6K+ 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 ... Read More

How to detect search engine bots with PHP?

AmitDiwan

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 ... Read More

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

AmitDiwan

AmitDiwan

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

914 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 ... Read More

Print newline in PHP in single quotes

AmitDiwan

AmitDiwan

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

639 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 ... Read More

How can I break an outer loop with PHP?

AmitDiwan

AmitDiwan

Updated on 06-Apr-2020 06:59:59

236 Views

If there are two nested loops, the break statement can be used −break 2;Below is a demonstration with the foreach loop −foreach(...) {    foreach(...) {       if (my_var_1.name == my_var_2)       break 2; //it breaks out of the outermost foreach loop    } }For PHP ... Read More

Advertisements