1 / 21

PHP (H ypertext P reprocessor )

PHP (H ypertext P reprocessor ). LECTURE 3. Prepared by: Muhammad Arif. PHP. PHP performs system functions, i.e. from files on a system it can create, open, read, write, and close them.

freya-hale
Télécharger la présentation

PHP (H ypertext P reprocessor )

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP (Hypertext Preprocessor) LECTURE 3 Prepared by: Muhammad Arif

  2. PHP • PHP performs system functions, i.e. from files on a system it can create, open, read, write, and close them. • PHP can handle forms, i.e. gather data from files, save data to a file, thru email you can send data, return data to the user. • You add, delete, and modify elements within your database thru PHP. • Access cookies variables and set cookies. • Using PHP, you can restrict users to access some pages of your website. • It can encrypt data

  3. Characteristics of PHP Five important characteristics make PHP's practical nature possible: • Simplicity • Efficiency • Security • Flexibility • Familiarity

  4. PHP Variables • PHP has a total of eight data types which we use to construct our variables: • Integers: are whole numbers, without a decimal point, like 4195. • Doubles: are floating-point numbers, like 3.14159 or 49.1. • Booleans: have only two possible values either true or false. • NULL: is a special type that only has one value: NULL. • Strings: are sequences of characters, like 'PHP supports string operations.‘ • Arrays: are named and indexed collections of other values. • Objects: are instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class. • Resources: are special variables that hold references to resources external to PHP (such as database connections).

  5. PHP is a Loosely Typed Language • In PHP a variable does not need to be declared before being set. i.e. $int_var = 12345; $many = 2.2888800; $my_var = NULL; $string_1 = "This is a string in double quotes"; • In the example above, you see that you do not have to tell PHP which data type the variable is. • PHP automatically converts the variable to the correct data type, depending on how they are set. • In a strongly typed programming language, you have to declare (define) the type and name of the variable before using it. • In PHP the variable is declared automatically when you use it.

  6. PHP Variable Scope • PHP has four different variable scopes: • Local • Global • Static • Parameter

  7. PHP Variable Scope • Local Scope • A variable declared within a PHP function becomes local and can only be accessed within that function. (the variable has local 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.

  8. PHP Variable Scope • Global Scope • Global scope refers to any variable that is defined outside of any function. • Global variables can be accessed from any part of the program that is not inside a function. • To access a global variable from within a function, use the global keyword: <?php$a = 10; // global scopefunction myTest(){ echo global $a; // reference to global scope variable} myTest();?> • PHP also stores all global variables in an array called $GLOBALS[ ]. • Its index is the name of the variable. This array is also accessible from within functions and can be used to update global variables directly: • global $a;$a="abc";

  9. PHP Variable Scope • Static 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. • Note: The variable is still local to the function.

  10. PHP Variable Scope • Parameters • 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. We will discuss them in more detail when we talk about functions.

  11. PHP Functions • To keep the script from being executed when the page loads, you can put it into a function. • A function will be executed by a call to the function. • You may call a function from anywhere within a page.

  12. PHP Built-in Functions • Array functions (e.g. array( ), array_fill( ), array_diff( ),…… etc) • Calendar functions (e.g. cal_days_in_month( ), cal_info( )……etc) • Date functions (e.g. checkdate( ), date( ), getdate( )……..etc) • Directory functions (e.g. chdir( ), dir( ),……etc) • Error functions (e.g. error_get_last ( ), get_log( )…..etc) • Filesystem functions • Filter functions • FTP functions • HTTP functions • LibXML functions • Mail functions • Math functions • Misc functions • MySQL functions • SimpleXML functions • String functions • XML Parser functions • Zip functions For built-in functions, please visit our PHP Reference.

  13. 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)

  14. Function Example <html><body><?php function writeName() { echo “shahid afridi"; }echo "My name is ";writeName(); ?></body></html> Output: My name is shahid afridi

  15. PHP Functions - Adding parameters • The following example will write different first names, but equal last name: <html><body><?phpfunction writeName($fname){echo $fname . " Refsnes.<br />";}echo "My name is ";writeName("Kai Jim");echo "My sister's name is ";writeName("Hege");echo "My brother's name is ";writeName("Stale");?></body></html> Output: My name is Kai Jim Refsnes. My sister's name is Hege Refsnes. My brother's name is Stale Refsnes.

  16. PHP Functions with Parameters: <html> <head> <title>Writing PHP Function with Parameters</title> </head> <body> <?php function addFunction($num1, $num2) { $sum = $num1 + $num2; echo "Sum of the two numbers is : $sum"; } addFunction(10, 20); ?> </body> </html>

  17. Passing Arguments by Reference: <?php function addFive($num) { $num += 5; } function addSix(&$num) { $num += 5; } $orignum = 10; addFive( &$orignum ); echo "Original Value is $orignum<br />"; addSix( $orignum ); echo "Original Value is $orignum<br />"; ?>

  18. PHP Functions - Return values • To let a function return a value, use the return statement. <html><body><?php function add($x,$y) { $total=$x+$y; return $total; }echo "1 + 16 = " . add(1,16);?></body></html> Output: 1 + 16 = 17

  19. PHP Functions returning value: <html><head> <title>Writing PHP Function which returns value</title></head> <body> <?php function addFunction($num1, $num2) { $sum = $num1 + $num2; return $sum; } $return_value = addFunction(10, 20); echo "Returned value from the function : $return_value ?> </body> </html>

  20. Setting Default Values for Function Parameters: • You can set a parameter to have a default value if the function's caller doesn't pass it. • Following function prints NULL in case use does not pass any value to this function. <?php function printMe($param = NULL) { print $param; } printMe("This is test"); printMe(); ?>

  21. Dynamic Function Calls: <?php function sayHello() { echo "Hello<br />"; } $function_holder = "sayHello"; $function_holder(); ?>

More Related