
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

2K+ Views
IntroductionThere is a peculiar behaviour of finally block when either try block or catch block (or both) contain a return statement. Normally return statement causes control of program go back to calling position. However, in case of a function with try /catch block with return, statements in finally block are executed first before returning.ExampleIn following example, div() function has a try - catch - finally construct. The try block without exception returns result of division. In case of exception, catch block returns error message. However, in either case statement in finally block is executed first.Example Live DemoOutputFollowing output is displayedThis block ... Read More

410 Views
IntroductionCode in finally block will always get executed whether there is an exception in ry block or not. This block appears either after catch block or instead of catch block.catch and finally blockIn following example, both catch and finally blocks are given. If execption occurs in try block, code in both is executed. If there is no exception, only finally block is executed.Example Live DemoOutputFollowing output is displayedCaught exception: Division by zero. This block is always executed Execution continueschange statement in try block so that no exception occursExample Live DemoOutputFollowing output is displayed2 This block is always executed Execution continuesfinally block onlyFollowing ... Read More

997 Views
IntroductionException class implements Throwable interface and is base class for all Exception classes, predefined exceptions as well as user defined exceptions. The Exception class defines some final (non-overridable) methods to implement those from Throwable interface, and __tostring() method that can be overridden to return a string representation of Exception object.final public function getMessage()message of exceptionfinal public function getCode()code of exceptionfinal public function getFile()source filenamefinal public function getLine()source linefinal public function getTrace()an array of the backtrace()final public function getPrevious()previous exceptionfinal public function getTraceAsString()formatted string of tracepublic function __toString()formatted string for displayIf user defined exception class re-defines the constructor, it should call parent::__construct() to ensure ... Read More

653 Views
IntroductionSyntax of declare statement in PHP is similar to other flow control structures such as while, for, foreach etc.Syntaxdeclare (directive) { statement1; statement2; . . }Behaviour of the block is defined by type of directive. Three types of directives can be provided in declare statement - ticks, encoding and strict_types directive.ticks directiveA tick is a name given to special event that occurs a certain number of statements in the script are executed. These statements are internal to PHP and roughky equal to the statements in your script (excluding conditional and argument expressions. Any function can be associated ... Read More

2K+ Views
To find the maximum element in an array, the PHP code is as follows −Example Live DemoOutputThe highest value of the array is91A function named ‘get_max_value()’ is defined, that takes an array as parameter. Inside this function, the count function is used to find the number of elements in the array, and it is assigned to a variable −$n = count($my_array);The first element in the array is assigned to a variable, and the array is iterated over, and adjacent values in the array are compared, and the highest value amongst all of them is given as output −$max_val = $my_array[0]; for ... Read More

158 Views
To check if a given number is present in an infinite series or not, the PHP code is as follows −Example Live DemoOutputThe number is not present in the infinite seriesAbove, three variables are defined, and the function is called by passing these three values −$m = 3; $n = 5; $o = 9;A function named ‘contains_val’ is defined, that takes these three variables. The first two variables are compared, and if they are equal, ‘true’ is returned. Otherwise, if the different between the first two variables with the product of the third number is greater than 0 and the different ... Read More

244 Views
To find the first ‘n’ numbers that are missing in an array, the PHP code is as follows −Example Live DemoOutputThe missing values of the array are 1 2 3 4 5In the above code, a function named ‘missing_values´ is defined, that takes the array, length, and the first few numbers that are missing from the array.A variable is assigned to 0 and checked if the first few numbers that need to be found is 0 or more. If it is 0, it is incremented.A count is assigned to 0, and curr value is assigned to 1. Next, the count value ... Read More

185 Views
To change date format in PHP, the code is as follows −Example Live DemoOutput1970-01-01 00:00:00A function named ‘string_convert’ is used in PHP that takes a date as a parameter. The ‘strtotime’ function is used to convert English text timing into a UNIX timestamp −$sec = strtotime($my_date); $my_date = date("Y-m-d H:i", $sec); $my_date = $my_date . ":00";This date is printed on the screen. Outside the function, the date time is defined, and the function is called on this parameter and the output is displayed on the screen −echo $my_date;

2K+ Views
To find the minimum element in an array, the PHP code is as follows −Example Live DemoOutputThe lowest value of the array is 0A function named ‘get_min_value()’ takes an array as parameter. Inside this function, the count function is used to find the number of elements in the array, and it is assigned to a variable −$n = count($my_array);The first element in the array is assigned to a variable, and the array is iterated over, and adjacent values in the array are compared, and the lowest value amongst all of them is given as output −$min_val = $my_array[0]; for ($i = ... Read More

381 Views
To delete an element from the array using the unset function, the PHP code is as follows −Example Live DemoOutputAfter deleting the element, the array isArray ( [0] => Joe [1] => Ben [2] => Mary [3] => Barun [5] => Mona )Above, an array is defined, with multiple strings −$my_array = array("Joe", "Ben", "Mary", "Barun", "Sona", "Mona");The unset function is used, by specifying the index of the element in the array that needs to be deleted. After the element is deleted, the output/array is printed on the screen −unset($my_array[4]); print_r("After deleting the element, the array is"); print_r ($my_array);