Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
Prog...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
Cont...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
Refe...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
What...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
What...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
What...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
What...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
What...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
What...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
What...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
What...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
Writ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops
PHP ...
Upcoming SlideShare
Loading in …5
×

U3 php 1

236 views
85 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
236
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

U3 php 1

  1. 1. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops Programaci´on Web - Unidad 3: Programaci´on del lado del servidor - PHP 1 Mario Garza Fabre mgarzaf@upv.edu.mx Universidad Polit´ecnica de Victoria Cd. Victoria, Tamaulipas, M´exico. http://www.tamps.cinvestav.mx/~mgarza/UPV_WP/ Enero - Abril, 2014 Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 1/28
  2. 2. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops Contents I 1 Introduction 2 Writing PHP code 3 Variables 4 echo and print 5 Arrays 6 Operators 7 Conditional Statements 8 Switch Statement 9 Loops 10 Functions 11 Useful functions Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 2/28
  3. 3. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops Referencias principales La mayor parte del contenido de este material ha sido tomado (o adaptado) de las siguientes fuentes principales: http://www.w3schools.com/ Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 3/28
  4. 4. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops What is PHP? and Why PHP? PHP is an acronym for “PHP Hypertext Preprocessor” Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 4/28
  5. 5. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops What is PHP? and Why PHP? PHP is an acronym for “PHP Hypertext Preprocessor” Powerful tool for making dynamic and interactive Web pages quickly Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 4/28
  6. 6. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops What is PHP? and Why PHP? PHP is an acronym for “PHP Hypertext Preprocessor” Powerful tool for making dynamic and interactive Web pages quickly Server scripting language. PHP code is executed on the server, and the result is returned to the browser as plain HTML Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 4/28
  7. 7. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops What is PHP? and Why PHP? PHP is an acronym for “PHP Hypertext Preprocessor” Powerful tool for making dynamic and interactive Web pages quickly Server scripting language. PHP code is executed on the server, and the result is returned to the browser as plain HTML PHP files have extension “.php” Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 4/28
  8. 8. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops What is PHP? and Why PHP? PHP is an acronym for “PHP Hypertext Preprocessor” Powerful tool for making dynamic and interactive Web pages quickly Server scripting language. PHP code is executed on the server, and the result is returned to the browser as plain HTML PHP files have extension “.php” Widely-used, free, efficient, and easy to learn Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 4/28
  9. 9. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops What is PHP? and Why PHP? PHP is an acronym for “PHP Hypertext Preprocessor” Powerful tool for making dynamic and interactive Web pages quickly Server scripting language. PHP code is executed on the server, and the result is returned to the browser as plain HTML PHP files have extension “.php” Widely-used, free, efficient, and easy to learn Runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.) Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 4/28
  10. 10. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops What is PHP? and Why PHP? PHP is an acronym for “PHP Hypertext Preprocessor” Powerful tool for making dynamic and interactive Web pages quickly Server scripting language. PHP code is executed on the server, and the result is returned to the browser as plain HTML PHP files have extension “.php” Widely-used, free, efficient, and easy to learn Runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.) Compatible with almost all servers used today (Apache, IIS, etc.) Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 4/28
  11. 11. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops What is PHP? and Why PHP? PHP is an acronym for “PHP Hypertext Preprocessor” Powerful tool for making dynamic and interactive Web pages quickly Server scripting language. PHP code is executed on the server, and the result is returned to the browser as plain HTML PHP files have extension “.php” Widely-used, free, efficient, and easy to learn Runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.) Compatible with almost all servers used today (Apache, IIS, etc.) Supports a wide range of databases Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 4/28
  12. 12. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops Writing PHP code “<?php” and “?>” tells where the PHP code starts and ends. <!DOCTYPE html> <html> <body> <h1>My f i r s t PHP page</h1> <?php echo ” Hello ! ” ; ?> </body> </html> Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 5/28
  13. 13. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - Comments Comments will not be executed by PHP Comments can be added to explain the PHP, or to make the code more readable. Single-line comments start with // or # Multi-line comments start with /* and end with */ Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 6/28
  14. 14. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - Case Sensitivity User-defined functions, classes, and keywords (e.g. if, else, while, echo, etc.) are NOT case-sensitive <!DOCTYPE html> <html> <body> <?php ECHO ” Hello World!<br>” ; echo ” Hello World!<br>” ; EcHo ” Hello World!<br>” ; ?> </body> </html> Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 7/28
  15. 15. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - Case Sensitivity However; in PHP, all variables are case-sensitive. <!DOCTYPE html> <html> <body> <?php $ c o l o r=” red ” ; echo ”My car i s ” . $ c o l o r . ”<br>” ; echo ”My house i s ” . $COLOR . ”<br>” ; echo ”My boat i s ” . $coLOR . ”<br>” ; ?> </body> </html> Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 8/28
  16. 16. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - Variables PHP variables are “containers” for storing information A variable starts with the $ sign, followed by the name of the variable A variable name must start with a letter or the underscore character A variable name can only contain alpha-numeric characters and underscores Variable names are case sensitive ($y and $Y are two different variables) Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 9/28
  17. 17. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - Variables A variable is created the moment you first assign a value to it (PHP has no command for declaring a variable). <?php $txt=” Hello ! ” ; $x=5; $y =10.5; ?> Loosely Typed Language PHP automatically converts the variable to the correct data type, depending on its value. Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 10/28
  18. 18. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - Variables Scope In PHP, variables can be declared anywhere in the script. The scope of a variable is the part of the script where the variable can be referenced/used. PHP has three different variable scopes: local global static Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 11/28
  19. 19. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - Local and Global Variables Local Variables A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function. Global Variables A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function. Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 12/28
  20. 20. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - Local and Global Variables The global keyword is used to access a global variable from within a function. <?php $x=5; $y=10; f u n c t i o n myTest (){ g l o b a l $x , $y ; $y=$x+$y ; } myTest ( ) ; echo $y ; // outputs 15 ?> Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 13/28
  21. 21. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - Local and Global Variables PHP also stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable. <?php $x=5; $y=10; f u n c t i o n myTest (){ $GLOBALS[ ’ y ’ ]=$GLOBALS[ ’ x ’ ]+$GLOBALS[ ’ y ’ ] ; } myTest ( ) ; echo $y ; // outputs 15 ?> Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 14/28
  22. 22. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - Local and Global Variables The static Keyword Normally, when a function is completed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. <?php f u n c t i o n myTest (){ s t a t i c $x=0; echo $x ; $x++; } myTest ( ) ; myTest ( ) ; myTest ( ) ; ?> Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 15/28
  23. 23. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - Constants A constant is an identifier (name) for a simple value. The value cannot be changed during the script. A valid constant name starts with a letter or underscore (no $ sign before the constant name). <?php define ( ”GREETING” , ”Welcome ! ” ) ; echo GREETING ; ?> Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 16/28
  24. 24. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - echo echo can output one or more strings echo ”<h2>PHP!</h2>” ; echo ” Hello ” , ” world ” , ” ! ! ! ” ; echo $var1 , $var2 ; $val = 2; echo ” Value : $val ” ; // Value : 2 echo ’ Value : $val ’ ; // Value : $val echo $var ? ’ true ’ : ’ f a l s e ’ ; echo ’ Esta ’ , ’ es ’ , ’ una ’ , ’ cadena ’ ; echo ’ Esta ’ . ’ es ’ . ’ otra ’ . ’ cadena ’ ; Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 17/28
  25. 25. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - print print can only output one string print ( ” Hello ” ) ; print ”<h2>PHP i s fun !</h2>” ; print $var ; $val = 2; print ” Value : $val ” ; // Value : 2 print ’ Value : $val ’ ; // Value : $val print ’ Esta ’ . ’ es ’ . ’ otra ’ . ’ cadena ’ ; Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 18/28
  26. 26. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - Arrays An array stores multiple values in one single variable. $cars=array ( ” Volvo ” , ”BMW” ) ; echo ” I l i k e ” . $cars [ 0 ] . ” and ” . $cars [ 1 ] ; Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 19/28
  27. 27. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - Arrays An array stores multiple values in one single variable. $cars=array ( ” Volvo ” , ”BMW” ) ; echo ” I l i k e ” . $cars [ 0 ] . ” and ” . $cars [ 1 ] ; or the index can be assigned manually: $cars [0]= ” Volvo ” ; $cars [1]= ”BMW” ; Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 19/28
  28. 28. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - Associative Arrays Associative arrays are arrays that use named keys that you assign to them. $age=array ( ” Peter ”=>”35” , ”Ben”=>”37” , ”Joe”=>”43” ) ; Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 20/28
  29. 29. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - Associative Arrays Associative arrays are arrays that use named keys that you assign to them. $age=array ( ” Peter ”=>”35” , ”Ben”=>”37” , ”Joe”=>”43” ) ; or: $age [ ’ Peter ’ ]=”35” ; $age [ ’Ben ’ ]=”37” ; $age [ ’ Joe ’ ]=”43” ; Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 20/28
  30. 30. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - Associative Arrays Associative arrays are arrays that use named keys that you assign to them. $age=array ( ” Peter ”=>”35” , ”Ben”=>”37” , ”Joe”=>”43” ) ; or: $age [ ’ Peter ’ ]=”35” ; $age [ ’Ben ’ ]=”37” ; $age [ ’ Joe ’ ]=”43” ; Usage: echo ” Peter i s ” . $age [ ’ Peter ’ ] . ” years old . ” ; Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 20/28
  31. 31. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - Operators Arithmetic: +, -, *, /, % Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 21/28
  32. 32. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - Operators Arithmetic: +, -, *, /, % Assignment: =, +=, -=, *=, /=, %= Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 21/28
  33. 33. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - Operators Arithmetic: +, -, *, /, % Assignment: =, +=, -=, *=, /=, %= String Operators: Concatenation: . Concatenation assignment: .= Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 21/28
  34. 34. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - Operators Arithmetic: +, -, *, /, % Assignment: =, +=, -=, *=, /=, %= String Operators: Concatenation: . Concatenation assignment: .= Increment/Decrement: Pre-increment: ++$x Post-increment: $x++ Pre-decrement: ––$x Post-decrement: $x–– Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 21/28
  35. 35. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - Operators Arithmetic: +, -, *, /, % Assignment: =, +=, -=, *=, /=, %= String Operators: Concatenation: . Concatenation assignment: .= Increment/Decrement: Pre-increment: ++$x Post-increment: $x++ Pre-decrement: ––$x Post-decrement: $x–– Comparison: ==, ===, != (or <>), !==, >, <, >=, <= Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 21/28
  36. 36. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - Operators Arithmetic: +, -, *, /, % Assignment: =, +=, -=, *=, /=, %= String Operators: Concatenation: . Concatenation assignment: .= Increment/Decrement: Pre-increment: ++$x Post-increment: $x++ Pre-decrement: ––$x Post-decrement: $x–– Comparison: ==, ===, != (or <>), !==, >, <, >=, <= Logical: and, &&, or, ||, !, xor Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 21/28
  37. 37. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - if...elseif....else i f ( c o n d i t i o n 1 ){ // code to be executed i f c o n d i t i o n 1 i s true } e l s e i f ( c o n d i t i o n 2 ){ // code to be executed i f c o n d i t i o n 2 i s true } else { // code to be executed i f n e i t h e r // c o n d i t i o n 1 nor c o n d i t i o n 2 i s true } Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 22/28
  38. 38. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - Switch Statement $ f a v c o l o r=” red ” ; switch ( $ f a v c o l o r ){ case ” red ” : echo ”Your f a v o r i t e c o l o r i s red ! ” ; break ; case ” blue ” : echo ”Your f a v o r i t e c o l o r i s blue ! ” ; break ; case ” green ” : echo ”Your f a v o r i t e c o l o r i s green ! ” ; break ; default : echo ” . . . n e i t h e r red , blue , or green ! ” ; } Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 23/28
  39. 39. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - while - do while while $x=1; while ( $x<=5){ echo ”Number : $x <br>” ; $x++; } do while $x=1; do{ echo ”Number : $x <br>” ; $x++; } while ( $x <=5); Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 24/28
  40. 40. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - for loop for <?php for ( $x=0; $x<=10; $x++){ echo ”The number i s : $x <br>” ; } ?> Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 25/28
  41. 41. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - foreach loop The foreach loop works only on arrays, and is used to loop through each key/value pair in an array. foreach <?php $ c o l o r s = array ( ” red ” , ” green ” , ” blue ” , ” yellow ” ) ; foreach ( $ c o l o r s as $value ){ echo ” $value <br>” ; } ?> Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 26/28
  42. 42. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - User Defined Functions Block of statements that can be used repeatedly in a program. A function will not execute immediately when a page loads. A function will be executed by a call to the function. <?php f u n c t i o n functionName ( $arg1 , $arg2 ){ r e t u r n $arg1 + $arg2 ; r e t u r n i s o p t i o n a l } ?> Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 27/28
  43. 43. Introduction Writing PHP code Variables echo and print Arrays Operators Conditional Statements Switch Statement Loops PHP - Useful functions strlen() returns the length (the number of characters) of a string count() return the length (the number of elements) of an array sort() sorts arrays in ascending order rsort() sorts arrays in descending order asort() sorts associative arrays in ascending order, based on the value ksort() sorts associative arrays in ascending order, based on the key arsort() sorts associative arrays in descending order, based on the value krsort() sorts associative arrays in descending order, based on the key Programaci´on Web - U3: Programaci´on del lado del servidor - PHP 1 28/28

×