1 / 58

Introduction to PHP

Introduction to PHP. PHP Introduction. > PHP is a server-side scripting language > PHP scripts are executed on the server > PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.) ‏ > PHP is open source software

terrywebb
Download Presentation

Introduction to PHP

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. Introduction to PHP

  2. PHP Introduction > PHP is a server-side scripting language > PHP scripts are executed on the server > PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)‏ > PHP is open source software > PHP is free to download and use

  3. PHP Introduction > 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

  4. PHP Operators

  5. PHP Operators

  6. PHP Conditional Statements > Very often when you write code, you want to perform different actions for different decisions. > You can use conditional statements in your code to do this. > In PHP we have the following conditional statements...

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

  8. The following example will output "Have a nice weekend!" if the current day is Friday: PHP Conditional Statements

  9. Use the if....else statement to execute some code if a condition is true and another code if a condition is false. PHP Conditional Statements

  10. If more than one line should be executed if a condition is true/false, the lines should be enclosed within curly braces { } PHP Conditional Statements

  11. The following example will output "Have a nice weekend!" if the current day is Friday, and "Have a nice Sunday!" if the current day is Sunday. Otherwise it will output "Have a nice day!": PHP Conditional Statements

  12. Use the switch statement to select one of many blocks of code to be executed. PHP Conditional Statements

  13. PHP Conditional Statements

  14. PHP Arrays > An array variable is a storage area holding a number or text. The problem is, a variable will hold only one value. > An array is a special variable, which can store multiple values in one single variable.

  15. If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this: PHP Arrays

  16. PHP Arrays > 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.

  17. PHP Arrays 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

  18. PHP Numeric Arrays > A numeric array stores each array element with a numeric index. > There are two methods to create a numeric array.

  19. In the following example the index is automatically assigned (the index starts at 0): In the following example we assign the index manually: PHP Numeric Arrays

  20. In the following example you access the variable values by referring to the array name and index: The code above will output: PHP Numeric Arrays

  21. PHP Associative Arrays > With an associative array, each ID key is associated with a value. > When storing data about specific named values, a numerical array is not always the best way to do it. > With associative arrays we can use the values as keys and assign values to them.

  22. In this example we use an array to assign ages to the different persons: This example is the same as the one above, but shows a different way of creating the array: PHP Associative Arrays

  23. PHP Associative Arrays

  24. In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. PHP Multidimensional Arrays

  25. PHP Multidimensional Arrays

  26. PHP Multidimensional Arrays

  27. PHP Multidimensional Arrays

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

  29. PHP Loops > 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

  30. The while loop executes a block of code while a condition is true. The example below defines a loop that starts with i=1. The loop will continue to run as long as i is less than, or equal to 5. i will increase by 1 each time the loop runs: PHP Loops - While

  31. PHP Loops - While

  32. The do...while statement will always execute the block of code once, it will then check the condition, and repeat the loop while the condition is true. The next example defines a loop that starts with i=1. It will then increment i with 1, and write some output. Then the condition is checked, and the loop will continue to run as long as i is less than, or equal to 5: PHP Loops – Do ... While

  33. PHP Loops – Do ... While

  34. PHP Loops – Do ... While

  35. PHP Loops - For

  36. PHP Loops - For Parameters: > init: Mostly used to set a counter (but can be any code to be executed once at the beginning of the loop)‏ > condition: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends. > increment: Mostly used to increment a counter (but can be any code to be executed at the end of the loop)‏

  37. The example below defines a loop that starts with i=1. The loop will continue to run as long as i is less than, or equal to 5. i will increase by 1 each time the loop runs: PHP Loops - For

  38. PHP Loops - For

  39. For every loop iteration, the value of the current array element is assigned to $value (and the array pointer is moved by one) - so on the next loop iteration, you'll be looking at the next array value. PHP Loops - Foreach

  40. The following example demonstrates a loop that will print the values of the given array: PHP Loops - Foreach

  41. Winner of the most impressive slide award PHP Loops - Foreach

  42. Strings in PHP • Strings are sequences of characters that can be treated as a unit • Assigned to variables • Given as input to functions • Returned from functions • Sent as output to appear on your user's web page • The simplest way to specify a string in PHP is to enclose it in single or double quotes

  43. String functions • PHP provides a huge variety of functions for the munching and crunching of strings • This section presents the basic functions for inspecting, comparing, modifying, and printing strings • Note for C programmers: many of the PHP string function names are the same or similar to the corresponding C/C++ function names and perform the same tasks and take the same arguments

  44. Inspecting strings • int strlen (string str) returns the number of characters in the string • Knowing the string's length is particularly useful for situations in which we'd like to loop through a string character by character

  45. Comparison • If you are wanting to determine if one string is the same as another, you can use the equality operator (==) if you want to determine if the strings are exactly equal (including case) and you are sure that the values on both sides are strings (e.g. if one of the values is an integer, the other one may be converted to an integer to perform the comparison) • int strcmp (string str1, string str2) returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal • int strcasecmp ( string str1, string str2) is the same as strcmp except that the comparisons are done case-insensitive

  46. String replacement • mixed str_replace (mixed search, mixed replace, mixed subject) returns a string or an array with all occurrences of search in subject replaced with the given replace value • string substr_replace (string string, string replacement, int start [,int length]) replaces a copy of string delimited by the start and (optionally) length parameters with the string given in replacement • string strrev (string string) returns string, reversed • string str_repeat (string input, int multiplier) returns input_str repeated multiplier times

  47. Case functions • string strtolower ( string str) returns string with all alphabetic characters converted to lowercase • string strtoupper ( string string) returns string with all alphabetic characters converted to uppercase • string ucfirst (string str) returns a string with the first character of str capitalized, if that character is alphabetic • string ucwords (string str) returns a string with the first character of each word in str capitalized, if that character is alphabetic. • The definition of a word is any string of characters that is immediately followed by a whitespace • ucfirst and ucwords will not convert any characters to lower case. If you want to convert the string to all lowercase and have the first letter of each word uppercased, use ucwords(strtolower($str))

  48. PHP Functions - Parameters • Adding parameters... • > To add more functionality to a function, we can add parameters. A parameter is just like a variable. • > Parameters are specified after the function name, inside the parentheses.

  49. PHP Functions - Parameters

  50. PHP Functions - Parameters

More Related