- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 1046 Articles for PHP
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
2K+ Views
Using Ajax to download files is not considered to be a good idea. Instead, window.location = or document.location should be used.The 'window.location' has the following characteristics −JavaScript enabling is requiredIt doesn't need PHP.It helps show the content of the site, and redirects the user after a few seconds.Redirect can be dependent on any conditions such as −$success = 1 if ($success) { window.location.href = 'http://example.com'; }A variable named ‘success’ is assigned with the value of 1. When this condition is satisfied, the window.location is used.On running, the user is redirected to the website 'http://example.com'Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
2K+ Views
HTTP URLs can be used to behave like local files, with the help of PHP wrappers. The contents from a URL can be fetched through the file_get_contents() and it can be echoed. or read using the readfile function.Below is a sample code to do the same −$file = file_get_contents('http://example.com/'); echo $file;An alternative is demonstrated below −readfile('http://example.com/'); header('Content-type: text/xml'); //The correct MIME type has to be set before displaying the output.The asXML method also can be used. Below is a sample code −echo $xml->asXML(); or $xml->asXML('filename.xml'); //providing a name for the xml file.Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
119 Views
The below line of code can be used to display the header that the PHP code received via a browser −orExample$headers = getallheaders(); foreach($headers as $key=>$val){ echo $key . ': ' . $val . ''; }OutputThis will produce the following output −Host: www.websitename.com Content-Length: 180 Cache-Control: max-age=0 Origin: http://www.websitename.com Upgrade-Insecure-Requests: 1 Content-Type: application/x-www-form-urlencoded User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36 Accept: text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, image/apng, */*;q=0.8, application/signed-exchange;v=b3;q=0.9 Referer: http://www.writephponline.com/ Accept-Encoding: gzip, deflate Accept-Language: en-GB, en-US;q=0.9, en;q=0.8 Cookie: _ga=GA1.2.118418268.1575911248; _gid=GA1.2.1794052867.1576852059Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
2K+ Views
The array_map function can be used to get the length and the max function can be used to get the length of the longest string.Below is a code sample for the same −$max_len = max(array_map('strlen', $array));Example Live Demo$array = array("a", "Ab", "abcd", "abcdfegh", "achn"); $max_len = max(array_map('strlen', $array)); echo $max_len;OutputThis will produce the following output −8
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
695 Views
Two variables can’t be explicitly returned, they can be put in a list/array data structure and returned.Example Live Demofunction factors( $n ) { // An empty array is declared $fact = array(); // Loop through it for ( $i = 1; $i < $n; $i++) { // Check if i is the factor of // n, push into array if( $n % $i == 0 ) array_push( $fact, $i ); } // Return array return $fact; } // Declare a variable and initialize it $num = 12; // Function call $nFactors = factors($num); // Display the result echo 'Factors of ' . $num . ' are: '; foreach( $nFactors as $x ) { echo $x . ""; }OutputThis will produce the following output −Factors of 12 are: 1 2 3 4 6
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
4K+ Views
The glob function can be used to get the images from a specific folder. Below is a sample code for the same −The path to the image folder is specified and all the files that have a .png extension are extracted. They are displayed with the help of the foreach loop −$dir_name = "path/to/image/folder/"; $images = glob($dir_name."*.png"); foreach($images as $image) { echo ''; }Based on the folder that contains all images, the path of every image present inside the folder is returned.
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
228 Views
Use glob to find all files matching a pattern.function recursive_directory_removal($directory) { foreach(glob("{$directory}/*") as $file) { if(is_dir($file)) { recursive_directory_removal($file); } else { unlink($file); } } rmdir($directory); }On PHP version 5.3 and above, the following code can be used −$dir = ... array_walk(glob($dir . '/*'), function ($fn) { if (is_file($fn)) unlink($fn); }); unlink($dir);
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
3K+ Views
There is no difference between die and exit, they are the same.The PHP Manual for exit states −"This language construct is equivalent to die()."The PHP Manual for die states −"This language construct is equivalent to exit()."However, there is a small difference, i.e the amount of time it takes for the parser to return the token.
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
858 Views
The array_filter function can be used to match a partial value in an array. A callback can be provided, that helps in deciding which elements would remain in the array and which would be removed.When the callback returns false, it means the given element needs to be removed. Below is a code example demonstrating the same −$arr = array(0 => 'abc', 1 => 'def', 2 => 'ghijk', 3 => 'lmnxyz'); $results = array(); foreach ($arr as $value) { if (strpos($value, 'xyz') !== false) { $results[] = $value; } } if( empty($results) ) { echo 'No matches found.'; } else ... Read More