what is php? php stands for php: hypertext preprocessor php is a server-side scripting language,...

44
PHP Introduction

Upload: julian-evans

Post on 04-Jan-2016

243 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: 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

PHP Introduction

Page 2: 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

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

Page 3: 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

PHP IntroductionWhat is a PHP File?•PHP files can contain text, HTML tags and scripts

•PHP files are returned to the browser as plain HTML 

•PHP files have a file extension of ".php", ".php3", or ".phtml"

Page 4: 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

PHP IntroductionWhat is MySQL?•MySQL is a database server•MySQL is ideal for both small and large applications•MySQL supports standard SQL•MySQL compiles on a number of platforms•MySQL is free to download and use

PHP + MySQL•PHP combined with MySQL are cross-platform (you can develop in Windows and serve on a Unix platform)

Page 5: 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

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

Page 6: 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

PHP SYNTAXBasic PHP Syntax•A PHP script always starts with

<?php and ends with ?>.

A PHP script can be placed anywhere in the document.•On servers with shorthand-support, you can start a PHP script with <? and end with ?>.•For maximum compatibility, we recommend that you use the standard form (<?php) rather than the shorthand form.

<?php?>

Page 7: 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

PHP SYNTAX•A PHP file must have a .php extension.

•A PHP file normally contains HTML tags, and some PHP scripting code.

•We have an example of a simple PHP script that sends the text “WELCOME TO THE WORLD OF DYNAMICS" back to the browser:

Page 8: 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

PHP SYNTAX<html><body>

<?phpecho " WELCOME TO THE WORLD OF DYNAMICS ";?>

</body></html>

Page 9: 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

PHP SYNTAX•Each code line in PHP must end with a semicolon. The semicolon is a separator and is used to distinguish one set of instructions from another.

•The basic statement to output text with PHP is echo.

•ECHO is the most important statement of the php code.

•In the example above we have used the echo statement to output the text " WELCOME TO THE WORLD OF DYNAMICS ".

Page 10: 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

COMMENTS IN PHPIn PHP, we use // to make a one-line comment or /* and */ to make a comment block:

<html><body>

<?php//This is a comment echo " WELCOME TO THE WORLD OF DYNAMICS "; /*This isa commentblock*/?>

</body></html>

Page 11: 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

WHAT VERSION DO I HAVE<?php

//shows you the current version of php

phpinfo();

?>

Page 12: 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

OPERATORSCommon Operator List

+ -- addition- -- subtraction* -- multiplication/ -- division ( and % -- modulus)

= -- assignment== -- equality test=== -- identical (same value & same type)

Page 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

OPERATORS!= -- Not equal (and <> -- not equal)> -- greater than ( and >= greater than or equal to)< -- less than ( and <= less than or equal to)

OR -- this OR that

AND -- this AND that

! -- not

Page 14: 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

DIFF B/W EQUAL & ASSIGNMENT OPERATOR

Equal Operator:• Equal operators are use to check the value that first value is equal to the second value or not.•Generally the sign of equal operator is (=).•In php the sign of equal operator is (= =)

Example

if (a= =b)

Echo “both are equal”;

Else

Echo “both are not equal”;

Page 15: 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

DIFF B/W EQUAL & ASSIGNMENT OPERATOR

Assignment Operator:• Assignment operator is use to assign the value to variable.•There are different ways to assign the value in different languages.•In php the sign of assignment operator is (=).•In algebraic form (=) operator is use for equal sign to check the value is equal or not.

Page 16: 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

CONSTANT & VARIABLESConstant•Constant have a fixed value and its value set and may not be changed by the program as the program runs.

VariableVariable can change its value during the execution of the program.

Page 17: 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

VARIABLES IN PHPVariables are "containers" for storing information.

In algebraic form values are presents like

x=5 y=4 z=x+y

In this equation x hold the value of 5 and y holds the value of 4 and z holds the value of the addition of x and y.

Z contain the value 9.

These letters are called variables, and variables can be used to hold values or expressions like z=x+y

Page 18: 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

VARIABLES IN PHPAs with algebra, PHP variables are used to hold values or expressions.

Rules for PHP variable names:•Variables in PHP starts with a $ sign, followed by the name of the variable•The variable name must begin with a letter or the underscore character•A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )•A variable name should not contain spaces•Variable names are case sensitive (y and Y are two different variables)

Page 19: 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

DECLARING VARIABLES•PHP has no command for declaring a variable.•A variable is created the moment you first assign a value to it like

$std=ali;

$f_name=Ahmed;

$myClass=BSCS;•After the execution of the statement above, the variable myClass will hold the value BSCS.•If you want to create a variable without assigning it a value, then you assign it the value of null.

Page 20: 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

DECLARING VARIABLES•Let's create a variable containing a string, and a variable containing a number:

<?php$txt=“Welcome To PHP";$x=16;?>

Note: When you assign a text value to a variable, put quotes around the value.

Page 21: 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

DECLARING VARIABLES•All variable names (including arrays) begin with $ followed by alpha or underscore•No variable declaration •All variables dynamically typed•Legal

•$mystring•$my_string•$_mystring•$mystrings[0]

•Illegal•$1mystring

Page 22: 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

DECLARING VARIABLES•In PHP, a variable does not need to be declared before adding a value to it.

•In the example above, notice that we did not have to tell PHP which data type the variable is.

•PHP automatically converts the variable to the correct data type, depending on its value.

•In a strongly typed programming language, you have to declare (define) the type and name of the variable before using it.

Page 23: 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

DECLARING VARIABLESExample

<?php

$n1=40;

$n2=75;

$sum=$n1+$n2:

Echo “The sum of two numbers is :” . $sum;

?>

Page 24: 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

VARIABLE SCOPE IN PHPThe scope of a variable is the portion of the script in which the variable can be referenced.

PHP has four different variable scopes:

•local•global•static•parameter

Page 25: 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

VARIABLE SCOPE IN PHPLocal Variable ScopeA variable declared within a PHP function is local and can only be accessed within that function. (the variable has local scope):

<?php$a = 5; // global scope

function myTest(){echo $a; // local scope}

myTest();?>

Page 26: 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

VARIABLE SCOPE IN PHPLocal Variable Scope•The script above will not produce any output because the echo statement refers to the local scope variable $a, which has not been assigned a value within this scope.

•You can have local variables with the same name in different functions, because local variables are only recognized by the function in which they are declared.

•Local variables are deleted as soon as the function is completed.

Page 27: 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

VARIABLE SCOPE IN PHPGlobal Variable Scope•Global scope refers to any variable that is defined outside of any function.

•Global variables can be accessed from any part of the script that is not inside a function.

•To access a global variable from within a function, use the global keyword:

Page 28: 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

VARIABLE SCOPE IN PHPGlobal Variable ScopeExample:

<?php$a = 5;$b = 10;

function myTest(){global $a, $b;$c = $a + $b;}

myTest();echo $c;?>The script above will output 15.

Page 29: 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

VARIABLE SCOPE IN PHPStatic Variable Scope:•When a function is completed, all of its variables are normally deleted. However, sometimes you want a local variable to not be deleted.•To do this, use the static keyword when you first declare the variable

static $rememberMe;

•Then, each time the function is called, that variable will still have the information it contained from the last time the function was called.•The variable is still local to the function.

Page 30: 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

VARIABLE SCOPE IN PHPParameters•A parameter is a local variable whose value is passed to the function by the calling code.•Parameters are declared in a parameter list as part of the function declaration:

function myTest($para1,$para2,...){// function code}•Parameters are also called arguments.

Page 31: 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

PHP STRINGS VARIABLE•A string variable is used to store and manipulate text.

•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.

Page 32: 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

PHP STRINGS VARIABLEBelow, the PHP script assigns the text "Hello World" to a string variable called $txt:

<?php$txt="Hello World";echo $txt;?>

The output of the code above will be:

Hello World

Now, lets try to use some different functions and operators to manipulate the string.

Page 33: 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

PHP STRINGS VARIABLEThe 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:

Page 34: 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

PHP STRINGS VARIABLE<?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!

If we look at the code above you see that we used the concatenation operator two times. This is because we had to insert a third string (a space character), to separate the two strings.

Page 35: 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

PHP STRINGS VARIABLEThe strlen() function

The strlen() function is used to return the length of a string.

Let's find the length of a string:

<?phpecho strlen("Hello world!");?>The output of the code above will be: 12

Page 36: 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

PHP STRINGS VARIABLEThe strpos() function

•The strpos() function is used to search for a character/text within a string.

•If a match is found, this function will return the character position of the first match. If no match is found, it will return FALSE.

•Let's see if we can find the string "world" in our string:

Page 37: 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

PHP STRINGS VARIABLE<?phpecho strpos("Hello world!","world");?>

The output of the code above will be:

6

The position of the string "world" in the example above is 6. The reason that it is 6 (and not 7), is that the first character position in the string is 0, and not 1.

Page 38: 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

PHP STRINGS VARIABLEThe trim() function:PHP provides three functions that remove leading or trailing spaces in a string.

•The trim()function will strip (remove) leading or trailing spaces in a string•The ltrim() function removes only the leading spaces•The rtrim() function removes only the trailing spaces

NOTE:Leading mean thing before or the thing/space on right side.

Trailing mean at the last or left side of the string.

Page 39: 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

PHP STRINGS VARIABLEThe substr()function:•The substr()function returns part of a string based on the values of the start and length parameters

•The syntax for the substr() function is:substr(string, start, optional length);

•A positive number in the start parameter indicates how many character to skip at the beginning of the string

•A negative number in the start parameter indicates how many characters to count in from the end of the string

Page 40: 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

PHP STRINGS VARIABLE•A positive value in the in the length parameter determines how many characters to return

•A negative value in the length parameter skip that many characters at the end of the string and returns the middle portion

•If the length is omitted or is greater than the remaining length of the string, the entire remainder of the string is returned

Page 41: 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

PHP STRINGS VARIABLEExample$ExampleString = "woodworking project";

echo substr($ExampleString,4) . "<br />\n";echo substr($ExampleString,4,7) . "<br />\n";echo substr($ExampleString,0,8) . "<br />\n";echo substr($ExampleString,-7) . "<br />\n";echo substr($ExampleString,-12,4) . "<br />\n";

OUTPUT:

Page 42: 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

PHP STRINGS VARIABLESOME INPORTANT CASES OF STRINGS VARIABLES.•PHP provides several functions to manipulate the case of a string

•The strtoupper()function converts all letters in a string to uppercase•The strtolower()function converts all letters in a string to lowercase•The ucfirst()function ensures that the first character of a word is uppercase•The lcfirst()function ensures that the first character of a word is lowercase

Page 43: 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

PHP STRINGS VARIABLE•Functions to manipulate the case of a string:

•The ucwords()function changes the first character of each word

•Use the strtolower()function on a string before using the ucfirst()and ucwords() to ensure that the remaining characters in a string are in lowercase

•Use the strtoupper()function on a string before using the ucfirst() and ucwords() to ensure that the remaining characters in a string are in uppercase

Page 44: 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

PHP STRINGS VARIABLE•Check the detail of other functions and do it by your self as assignment.

Thanks