
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 1060 Articles for PHP

7K+ Views
In PHP, the 'exec()' or 'shell_exec’ function can be used. It can be executed via the shell and the result can be returned as a string. It returns an error if NULL is passed from the command line or returns no output at all. 'exec()' Function The exec function is used for executing commands in the shell and command line of an external program and optinally capture the last line of the output. Syntax exec(string $command, array &$output = null, int &$return_var = null); 'shell_exec()' Function The shell_exec is similiar to exec but it captures and return the ... Read More

778 Views
This is not possible. A href can’t be hidden from a link. But the files can be rewritten and the request URL can be changed to look like this − name.php/5001Other than this, a post request can be used in the below way − Go This will expose a single button in the browser.

1K+ Views
Method 1$arr_main_array = array('test_val' => 123, 'other-value' => 456, 'test_result' => 789); foreach($arr_main_array as $key => $value){ $exp_key = explode('-', $key); if($exp_key[0] == 'test'){ $arr_result[] = $value; } } if(isset($arr_result)){ print_r($arr_result); }Method 2A functional approach An array_filter_key type of function is taken, and applied to the array elements $array = array_filter_key($array, function($key) { return strpos($key, 'foo-') === 0; });Method 3A procedural approach −$val_1 = array(); foreach ($array as $key => $value) { if (strpos($key, 'foo-') === 0) { $val_1[$key] = $value; } }Method 4A procedural approach using ... Read More

5K+ Views
Images can be resized using ImageMagick or GD functions. If GD’s functions are used, the size of the image file is also reduced when raw digital camera images are sampled. We will see how GD can be used to resize an image in the below code.function image_resize($file_name, $width, $height, $crop=FALSE) { list($wid, $ht) = getimagesize($file_name); $r = $wid / $ht; if ($crop) { if ($wid > $ht) { $wid = ceil($wid-($width*abs($r-$width/$height))); } else { $ht = ceil($ht-($ht*abs($r-$w/$h))); } ... Read More

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

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

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 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

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] => 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]

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 being run (which is not running via the web server).