0% found this document useful (0 votes)
84 views

PHP Report

The document provides an overview of PHP including: - What PHP is and why it's useful for web development - PHP syntax like comments, variables, and data types - Operators, conditional statements, arrays, loops, and functions - Form handling using the $_GET, $_POST, and $_REQUEST superglobals - The include() function for including files It serves as a basic introduction to the PHP language, its capabilities, and common constructs.

Uploaded by

Richa Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

PHP Report

The document provides an overview of PHP including: - What PHP is and why it's useful for web development - PHP syntax like comments, variables, and data types - Operators, conditional statements, arrays, loops, and functions - Form handling using the $_GET, $_POST, and $_REQUEST superglobals - The include() function for including files It serves as a basic introduction to the PHP language, its capabilities, and common constructs.

Uploaded by

Richa Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 13

What is PHP?

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

block */ ?> </body> </html>

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; ?>

Naming Rules for Variables


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 in PHP


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

This section lists the different operators used in PHP.

Arithmetic Operators Operator Description + Addition

* / %

Subtraction Multiplication Division Modulus (division remainder)

++ -

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

Result 4 3 20 3 2.5 1 2 0 x=6 x=4

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

5<8 returns true 5>=8 returns false 5<=8 returns true

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:

Hello World! What a nice day!

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

task like this. In PHP, we have the following looping statements:


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.

Create a PHP Function


A function will be executed by a call to the function. Syntax function functionName() { code to be executed; } PHP function guidelines:

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

PHP Form Handling


The most important thing to notice when dealing with HTML forms and PHP is that any form element in an HTML page will automatically be available to your PHP scripts. Example The example below contains an HTML form with two input fields and a submit button: <html> <body> <form action="welcome.php" method="post"> Name: <input type="text" name="fname" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> </body> </html> When a user fills out the form above and click on the submit button, the form data is sent to a PHP file, called "welcome.php": "welcome.php" looks like this: <html> <body> Welcome <?php echo $_POST["fname"]; ?>!<br />

You are <?php echo $_POST["age"]; ?> years old. </body> </html> Output could be something like this: Welcome John! You are 28 years old.

PHP Forms and User Input

The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input.

PHP $_GET Function


The built-in $_GET function is used to collect values from a form sent 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://www.w3schools.com/welcome.php?fname=Peter&age=37 The "welcome.php" file can now use the $_GET function to collect form data (the names of

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!

When to use method="get"?


When using method="get" in HTML forms, all variable names and values are displayed in the URL. Note: This method should not be used when sending passwords or other sensitive information! However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases. Note: The get method is not suitable for very large variable values. It should not be used with values exceeding 2000 characters.

PHP $_POST Function

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.

When to use 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.However, because the variables are not displayed in the URL, it is not possible to bookmark the page.

The PHP $_REQUEST Function


The PHP built-in $_REQUEST function contains the contents of both $_GET, $_POST, and $_COOKIE. The $_REQUEST function can be used to collect form data sent with both the GET and POST methods. Example Welcome <?php echo $_REQUEST["fname"]; ?>!<br /> You are <?php echo $_REQUEST["age"]; ?> years old.

PHP include() Function


The include() function takes all the content in a specified file and includes it in the current file. If an error occurs, the include() function generates a warning, but the script will continue execution. Example 1 Assume that you have a standard header file, called "header.php". To include the header file in a page, use the include() function: <html> <body> <?php include("header.php"); ?>

<h1>Welcome to my home page!</h1> <p>Some text.</p> </body> </html>

PHP require() Function


The require() function is identical to include(), except that it handles errors differently. If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop. Error Example include() Function <html><body> <?php include("wrongFile.php"); echo "Hello World!"; ?> </body> </html> Error message: Warning: include(wrongFile.php) [function.include]: failed to open stream: No such file or directory in C:\home\website\test.php on line 5 Warning: include() [function.include]: Failed opening 'wrongFile.php' for inclusion (include_path='.;C:\php5\pear') in C:\home\website\test.php on line 5 Hello World! Notice that the echo statement is executed! This is because a Warning does not stop the script execution

PHP Exception Handling

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

We will show different error handling methods:


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

You might also like