To detect the existence of a table, use the concept of INFORMATION_SCHEMA.TABLES. Following is the syntax −select table_name from information_schema.tables where table_schema=database() and table_name=yourTableName;To understand the above syntax, let us create a table −mysql> create table DemoTable2032 -> ( -> ClientId int, -> ClientName varchar(20), -> ClientAge int, -> ClientCountryName varchar(20) -> ); Query OK, 0 rows affected (1.07 sec)Here is the query to detect if a table exist in a database −mysql> select table_name from information_schema.tables -> where table_schema=database() -> and table_name='DemoTable2032';This will produce the following output ... Read More
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.
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);
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.
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
SOAP and REST APIs are the widely used APIs.Consider the presence of a PHP class named manage.php that helps in managing the entries in a database.class manage { private $entryId; function __construct($entryId) { $this->entryId = $entryId; } function deleteEntry() { //delete $this->entryId from database }}On the server, this functionality can be accessed as shown below −require_once('manage.php'); $m = new manage(12); $m->deleteEntry();How can this be accessed by a different server? A third file can be created that will behave like a buffer/an interface that helps access this data. Below is a sample buffer −Let us call it ‘api/delete.php’require_once('manage.php'); if(hasPermission($_POST['api_key']) ... Read More
The return value of strtolower can be passed as the third argument to str_replace (where $string is present). The str_replace function is used to replace a set of characters/character with a different set of character/string.Example Live Demo$str = 'hello have a good day everyone'; echo str_replace(' ', '-', strtolower($str));OutputThis will produce the following output −hello-have-a-good-day-everyone
Yes, it can be done using the unlink function. It has been shown below −Another alternative that deletes the script irrespective of whether the exit function is called or not, has been shown below ^minus;class DeleteOnExit { function __destruct() { unlink(__FILE__); } } $delete_on_exit = new DeleteOnExit();
The below code can be used to parse JSON array −Example Live Demo
Regular expression$directory = new RecursiveDirectoryIterator(__DIR__); $flattened = new RecursiveIteratorIterator($directory); // Make sure the path does not contain "/.Trash*" folders and ends eith a .php or .html file $files = new RegexIterator($flattened, '#^(?:[A-Z]:)?(?:/(?!\.Trash)[^/]+)+/[^/]+\.(?:php|html)$#Di'); foreach($files as $file) { echo $file . PHP_EOL; }Using filtersA base class holds the regex that needs to be used with the filter.classes which will extend this one. The RecursiveRegexIterator is extended.abstract class FilesystemRegexFilter extends RecursiveRegexIterator { protected $regex; public function __construct(RecursiveIterator $it, $regex) { $this->regex = $regex; parent::__construct($it, $regex); } }They are basic filters and work ... Read More