Skip to main content

Posts

Form Validation and user inputs

User input should be validated on the browser whenever possible (by client scripts). Browser validation is faster and reduces the server load. The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input. The $_GET Variable The predefined $_GET variable is used to collect values in a form with method=”get” Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser’s address bar) and has limits on the amount of information to send. Example <form action=”welcome.php” method=”get”> Name: <input type=”text” name=”fname” /> Age: <input type=”text” name=”age” /> <input type=”submit” /> </form> When the user clicks the “Submit” button, the URL sent to the server could look something like this: http://localhost/welcome.php?fname=Peter&age=37 The “welcome.php” file can now use the $_GET variable to collect form data (the names of the form fields will ...

PHP in-built functions

PHP Basic Functions   Function Description array() Creates an array array_push() Inserts one or more elements to the end of an array shuffle() Shuffles an array sizeof() Alias of count() sort() Sorts an array count() Counts elements in an array, or properties in an object uasort() Sorts an array with a user-defined function and maintain index association date() Formats a local time/date getdate() Returns an array that contains date and time information for a Unix timestamp mktime() Returns the Unix timestamp for a date strftime() Formats a local time/date according to locale settings strptime() Parses a time/date generated with strftime() time() Returns the current time as a Unix timestamp chdir() Changes the current directory dir() Opens a directory handle and returns an object addcslashes() Returns a string with backslashes in front of the specified characters addslashes() Returns a string with backslashe...

PHP functions

A function is a block of code that can be executed whenever required. A function will be executed by a call to the function. You may call a function from anywhere within a page. Give the function a name that reflects what the function does. The function name can start with a letter or underscore but not a number. Syntax function functionName() { code to be executed } Creating a simple function <?php function hello(){ echo “This is my first simple function”; } //call php function hello(); ?> PHP function with parameters <?php function myHello($firstname) { echo “Namaste “. $firstName . “!<br />”; } //call function myHello(“Ram”); ?> Display: Namaste Ram ! Another example <?php function addNumber($num1, $num2) { $num3= $num1+$num2; echo “The sum is”.$num3; } //function call addNumber(15,25); ?>

Conditional Statements

There are three types of conditional statements. They are as follows: if statement if else statement switch statement 1) if statement : This statement is used to execute some code only if a specified condition is true. Syntax: if(condition) { write statement here }  2) if….else statement: If a condition is true, one code is executed and another code if the condition is false. Syntax: if(condition) { write statement here } else if(condition) { write statement here } else { write statement here } 3) switch statement: This statement is used to select one of many blocks of code to be executed. Syntax: switch(variable) { case 1: write statement to be executed break; case 2: write statement to be executed break; case 3: write statement to be executed break; default: write statement to be executed } Examples  1) if statement <?php $day=”Fri”; if($day==”Fri”) { echo  “Have...

PHP Loops

There are four types of loops in PHP. They are as follows: 1) while loop 2) do…while loop 3) for loop 4) foreach loop  1) while loop: Loops through a block of codes while a specified condition is true. Example: <?php $i=1; while($i<=5) { echo  “The number is ”.$i.”<br/>”; $i++; } ?> 2) do…while loop: Loops through a block of code once and then repeats the loop as long as a specified condition is true. Example: <?php $i=1; do{ $i++; echo “The number is ”.$i.”<br/>”; } while($i<=5); ?> 3)for loop: Loops through a block of code a specified number of items. Example: <?php for($i=1;$i<=5;$i++) { echo “The number is ”.$i.”<br/>”; } ?> 4) foreach loop: Loops through a block of code for each element in an array. Example: <?php $x=array(“one”,”two”,”three”); foreach($x as $value) { echo $x.”<br/>”; } ?>

PHP Operators

S.no Operator Operation 1 Addition (+) 4+5=9 2 Subtraction (-) 10-2=8 3 Multiplication (*) 2*4=8 4 Division (/) 15/5=3 5 Modulus (%) 5%2=1, 10%8=2, 10%2=0 6 Increment (++) x=5 x++ is equivalent to x=x+1 x=6 7 Decrement (--) x=5 x—is equivalent to x=x-1 x=4

Variables in PHP

Variables are used for storing a values, like text strings, numbers or arrays. When a variable is declared, it can be used over and over again in your script. All variables in PHP start with a ‘$’sign symbol. $var_name=value; For example: <?php $txt=”Hello world”; echo $txt; ?>