PHP Report
PHP Report
PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.) PHP is an open source software PHP is free to download and use
Why PHP?
PHP runs on different platforms (Windows, Linux, Unix, etc.) PHP is compatible with almost all servers used today (Apache, IIS, etc.) PHP is FREE to download from the official PHP resource: www.php.net PHP is easy to learn and runs efficiently on the server side
PHP SYNTAX
A PHP scripting block always starts with <?php and ends with ?>. A PHP scripting block can be placed anywhere in the document. <html> <body> <?php echo "Hello World"; ?> </body> </html>
Comments in PHP
In PHP, we use // to make a single-line comment or /* and */ to make a large comment block. <html> <body> <?php //This is a comment /* This is a comment
Variables in PHP
Variables are used for storing values, like text strings, numbers or arrays. All variables in PHP start with a $ sign symbol.The correct way of declaring a variable in PHP: $var_name = value; Ex: <?php $txt="Hello World!"; $x=16; ?>
A variable name must start with a letter or an underscore "_" A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ ) A variable name should not contain spaces. If a variable name is more than one word, it should be separated with an underscore ($my_string), or with capitalization ($myString)
String variables are used for values that contain characters. After we create a string we can manipulate it. A string can be used directly in a function or it can be stored in a variable. Ex:
<?php $txt="Hello World"; echo $txt; ?> The output of the code above will be: Hello World
PHP Operators
* / %
++ -
Increment Decrement
Example x=2 x+2 x=2 5-x x=4 x*5 15/5 5/2 5%2 10%8 10%2 x=5 x++ x=5 x-Is The Same As x=y x=x+y x=x-y x=x*y x=x/y x=x.y x=x%y Example 5==8 returns false 5!=8 returns true 5<>8 returns true 5>8 returns false
Assignment Operators Operator Example = x=y += x+=y -= x-=y *= x*=y /= x/=y .= x.=y %= x%=y Comparison Operators Operator Description == is equal to != is not equal <> is not equal > is greater than
is less than is greater than or equal to is less than or equal to Logical Operators Description and
Operator &&
Example x=6 y=3 (x < 10 && y > 1) returns true x=6 y=3 (x==5 || y==5) returns false x=6 y=3 !(x==y) returns true
||
or
not
Conditional statements are used to perform different actions based on different conditions.
Concatenation Operator
There is only one string operator in PHP. The concatenation operator (.) is used to put two string values together. To concatenate two string variables together, use the concatenation operator: <?php $txt1="Hello World!"; $txt2="What a nice day!"; echo $txt1 . " " . $txt2; ?> The output of the code above will be:
Conditional Statements
In PHP we have the following conditional statements:
if statement - use this statement to execute some code only if a specified condition is true
if...else statement - use this statement to execute some code if a condition is true and another code if the condition is false if...elseif....else statement - use this statement to select one of several blocks of code to be executed switch statement - use this statement to select one of many blocks of code to be executed
PHP Arrays
An array is a special variable, which can store multiple values in one single variable. If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this: $cars1="Saab"; $cars2="Volvo"; $cars3="BMW"; However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?The best solution here is to use an array! An array can hold all your variable values under a single name. And you can access the values by referring to the array name.Each element in the array has its own index so that it can be easily accessed. In PHP, there are three kind of arrays:
Numeric array - An array with a numeric index Associative array - An array where each ID key is associated with a value Multidimensional array - An array containing one or more arrays
PHP Loops
Often when you write code, you want the same block of code to run over and over again in a row. Instead of adding several almost equal lines in a script we can use loops to perform a
while - loops through a block of code while a specified condition is true do...while - loops through a block of code once, and then repeats the loop as long as a specified condition is true for - loops through a block of code a specified number of times foreach - loops through a block of code for each element in an array
PHP Functions
The real power of PHP comes from its functions.In PHP, there are more than 700 built-in functions.
Give the function a name that reflects what the function does The function name can start with a letter or underscore (not a number)
Example A simple function that writes my name when it is called: <html> <body> <?php function writeName()
{ echo "Kai Jim Refsnes"; } echo "My name is "; writeName(); ?> </body> </html> Output: My name is Kai Jim Refsnes
You are <?php echo $_POST["age"]; ?> years old. </body> </html> Output could be something like this: Welcome John! You are 28 years old.
The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input.
the form fields will automatically be the keys in the $_GET array): Welcome <?php echo $_GET["fname"]; ?>.<br /> You are <?php echo $_GET["age"]; ?> years old!
The built-in $_POST function is used to collect values from a form sent with method="post". Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send. Note: However, there is an 8 Mb max size for the POST method, by default (can be changed by setting the post_max_size in the php.ini file).
Example
<form action="welcome.php" method="post"> 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 will look like this: http://www.w3schools.com/welcome.php
The "welcome.php" file can now use the $_POST function to collect form data (the names of the form fields will automatically be the keys in the $_POST array): Welcome <?php echo $_POST["fname"]; ?>!<br /> You are <?php echo $_POST["age"]; ?> years old.
Exceptions are used to change the normal flow of a script if a specified error occurs
What is an Exception
With PHP 5 came a new object oriented way of dealing with errors. Exception handling is used to change the normal flow of the code execution if a specified error (exceptional) condition occurs. This condition is called an exception. This is what normally happens when an exception is triggered:
The current code state is saved The code execution will switch to a predefined (custom) exception handler function Depending on the situation, the handler may then resume the execution from the saved code state, terminate the script execution or continue the script from a different location in the code
Basic use of Exceptions Creating a custom exception handler Multiple exceptions Re-throwing an exception Setting a top level exception handler
Note: Exceptions should only be used with error conditions, and should not be used to jump to another place in the code at a specified point.
PHP DATABASE
PHP
support variety of database management systems, including : - MySQL - Oracle - PostgreSQL - Microsoft Access