Error Handling in PHP

php-training-in-gurgaon
Error handling is the process of catching errors from the program and then taking appropriate action. Before proceeding for error handling you should get to know how many types of errors occurred in PHP. Notices: These are non-critical errors that occurred while executing a script - for example, accessing a variable that has not yet been defined. By default, such errors are not displayed to the user at all - although, as you will see, you can change this default behavior. Warnings: These are more serious errors - for example, attempting to use a file (using include() method) which does not exist. By default, these errors are displayed to the user, but they do not result in script termination. Fatal Errors: These are critical errors - for example, creating an…
Read More

PHP – Strings Functions

php-training-in-gurgaon
String functions are the most important functions frequently used in PHP. String is the sequence of characters, like "Let’s learn String functions”. There is no installation required to use string function. Example <?php $name = "Hirdesh Bhardwaj"; print($name); ?> String Concatenation Operator To concatenate two string variables together, we use the dot (.) operator − <?php $txt1="Hello Readers"; $txt2="8756"; echo $txt1 . " " . $txt2; ?> Using the strlen() function The strlen() function is used to get the length of a string. <?php $txt=”Webs Jyoti”; echo strlen($txt); ?> This will produce the following result – 10 (including space) Using the strpos() function The strpos() function is used to search for a text or character within a string. If the value is found then this will return the position of…
Read More

Working with Images – PHP

php-training-in-gurgaon
We need to create an HTML form that allows you to choose the file to be uploaded. Note: please make sure you have form method type is post and enctype=”mutipart/form-data” to to handle file uploading process successfully. HTML Code <form enctype="multipart/form-data" action="process.php" method="POST"> Choose File : <input name="myfile" type="file" /> <input type="submit" value="Upload" /> </form> Now create a PHP file process.php for handling file information <?php // get the uploaded file name $filename= $_FILES['myfile'][‘name’]; // get the file type $filetype= $_FILES['myfile'][‘type’]; // get the file size (in bytes) $filesize= $_FILES['myfile'][‘size’]; // You may put your validations here i.e restrict file size if($filesize>=1000000) { echo “ File is too Large, Maximum file size should be 1 MB”; exit; } // Validation to check if uploaded file is an Image if($filetype !=”image/jpeg”…
Read More

The do-while loop

php-training-in-gurgaon
Difference Between while and do…while Loop: With a while loop, the condition to be evaluated is tested at the beginning of each loop statement, so if the conditional expression evaluates to false, the loop will never be executed. With a do-while loop, on the other hand, the loop will always be executed once, even if the conditional expression is false, because the condition is evaluated at the end of the loop statement. Example: <?php $i = 1; do { $i++; echo  $i . "<br>"; } while($i <= 3); ?>  Output: 2 3 4
Read More

The While Loop

php-training-in-gurgaon
A While loop is used when we have to repeat a set of statements as long as the condition is false. It could be the best where number of repetition is not known earlier or in advance. It can be explain in plain English as "Keep doing something until a condition is true". Syntax: while (condition) { //  Set of statements goes here; } Example <?php $ct=5; while($ct<=50) { echo  $ct . "<br>"; $ct = $ct+5; } ?> Output: 5 10 15 20 25 30 35 40 45 50 The do…while loop example: <?php $i = 1; do { $i++; echo  $i . "<br>"; } while($i <= 3); ?>  Output: 2 3 4 Difference Between while and do…while Loop: With a while loop, the condition to be evaluated is tested…
Read More

PHP -Building Blocks

php-training-in-gurgaon
If –else statements: if…else statement – executes set of codes if a condition is true and another code if the condition is false Within a statement, if a certain condition is true, then something happens. Otherwise, something else happens. We can continue adding else statements as many times as we need. So, within PHP statement, we first define the variable and then set up the conditional statements like this: Syntax: if (condition) code to be executed if condition is true; elseif (condition) code to be executed if condition is true; else code to be executed if condition is false; Example 1: <?php $avg = 50 if ($avg>60) { echo "You have a good score"; } else { echo "Score is less than average"; } ?> Example 2:  <?php $status =…
Read More

The For Loop

php-training-in-gurgaon
PHP for loop can be used to traverse set of code for the specified number of times. For example if you want to repeat something ten or twenty times. It should be used if number of iteration is known otherwise I recommend you to use while loop. Syntax: for (Initialization; Condition; Progressive) { //set of statements to be executed; } Parameters: Initialization: Initialize the loop counter value Condition: Check for condition. If it evaluates to TRUE, the loop continues. If FALSE then the loop ends. Progressive: Increase / Decrease the loop counter value Example: for($var=1; $var<=20; $var=$var+1) { echo $var ." "; } Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Example: to display 1st 20 natural…
Read More

PHP Superglobals

php-training-in-gurgaon
We already learned about variables, but there are some Readymade or predefined keywords in PHP that can be used without you having to create them first. Predefined or Superglobals variables are used to provide information from and about the web server, the web browser, and the user. There are several types of Super Global Variables:- $_GET - Is used to pass data through URL. Example: http://websjyoti.com?id=2 $_POST - Is used to pass data to server by hiding from URL. $_REQUEST – Is the combination of $_GET, $_POST, and $_COOKIE. $_SESSION - is used to maintain session and data from Local machine to Server. $_COOKIE – Is used to save/get user data on same machine in browser cookies. $_SERVER – Is an associative array which is used to know server information…
Read More

Arrays in PHP

php-training-in-gurgaon
An array is a data structure that stores similar type of values in a common variable. Arrays are also called homogeneous data type. For example if you want to store 50 numbers then instead of defining variables it is easy to define an array with size of 50. In PHP, the array() function is used to create an array Arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero. $x=array("laxmi","hirdesh","rahim","ashu","amit"); // To count number of elements stored in array x echo count($x); // To populate value from any specific index or position //echo $x[1]; // output – Hirdesh // To print all elements in array x - using for-each loop echo " <br>Names are :"; foreach ($x…
Read More