CFPHP ZCE Training
    Session One: PHP Basics




                              1
In Today’s Episode
•                           •
    PHP Files                   Summary

•                           •
  ...
PHP Files



            3
Scripts

• Default Extension: .php
 • May be Overridden via Server Conf
• Not Compiled, Interpreted
 • Thus, PHP is a text...
Tags

• Standard Tags
 • <?php ?>
• Short Tags
 • <? ?>

                         5
Tags
• Script Tags
 • <script language=“PHP”></script>
• ASP Tags
 • <% %>
• * Standard tags are the only
  recommendation...
Newlines

• Interpreter removes newlines at end of all
  scripts
• This prevents “Headers Already Sent”
  errors




     ...
Statement Termination
• Semicolons terminate PHP statements
 • function_call();
 • $a = 123;
• There are few exceptions
 •...
Comments

• // Inline Comment
• # PERL Style Comment
• /* C Style
  Multiline
  Comment */



                         9
Whitespace
• PHP = Loosely Typed
• Whitespace is usually ignored
• Exceptions:
 • Opening/Closing Tags
 • Keywords
 • Func...
Code Blocks
• Defined by { Curly Braces }
• Groups code into related blocks
• Runs under specific circumstances
 • Functions...
Language Constructs

• Similar to Functions... different rules
• if(), while(), for(), foreach(), function()
• echo() vs p...
Data Types



             13
Data Types

• 2 Main Categories
 • Scalar Values
 • Compound Values


                      14
Data Types

• Numeric Types
 • Integer
 • Floating Point


                       15
Data Types
• Integers
 • Sined values only
 • Defined Via
    • Decimal Notation
    • Octal Notation
    • Hexadecimal Not...
Data Types

• Floating Point
 • AKA: Doubles, Floats
 • Defined Via:
    • Decimal Notation
    • Exponential Notation

   ...
Data Types
• Strings
 • Junkdrawers for Binary Data (Usually
    Text)
 • Single Quotes = Simple Strings
 • Double Quotes ...
Data Types

• Booleans
 • TRUE
 • FALSE


                          19
Data Types

• Compound Data Types
 • Array
 • Object


                        20
Data Types

• Other Data Types
 • null
 • Resources


                       21
Variables & Constants



                        22
Syntax

• Defined by $
• Names may be alpha-numeric
• May contain underscores
• May NOT begin with a Number

              ...
Variable Variables

• Variable Name becomes a variable
• Generally not recommended
• Code Difficult to read


             ...
Variable Existance


• isset()



                       25
Constants

• Similar to variables, but not
• Values Stored once, may never be
  changed
• Names are suggested to be in ALL...
Operators



            27
Arithmetic

• Addition: +
• Subtraction: -
• Multiplication: *
• Division: /
• Modulus: %

                         28
Arithmetic

• Increment Before: ++$a
• Increment After: $a++
• Decrement Before: --$a
• Decrement After: $a--

           ...
String Concatenation

• Period is only Concatenation Operator
• Two Ways to Concatenate
 • $c = $a . $b;
 • $a .= $b;

   ...
Assignment

• Simple Assignment
• Arithmetic/Bitwise Assignment
 • $a += $b;
 • $a *= $b;

                               ...
References

• $a = 4;
  $b = $a;
  $a = 6;

  echo $b;




                          32
References

• $a = 4;
  $b &= $a;
  $a = 6;

  echo $b;




                          33
References


• Scalar Values and Arrays are always
  COPIED
• Objects are Passed Via Reference


                         ...
Comparison
• Results of Comparison Operations are
  ALWAYS boolean
• Equal ==
• Identical ===
• Not Equal !=
• Not Identic...
Comparison

• Less Than <
• Greater Than >
• Less Than or Equal To <=
• Greater Than or Equal To >=

                     ...
Logical

• NOT !
• AND && / and
• OR || / or
• XOR

                      37
Other

• Error Control Operator @
 • @mysql_connect(‘foo’, ‘bar’, ‘beer’);
• Backtick
 • $a = `ls -la`;

                 ...
Control Structures



                     39
Conditionals

• if
• else
• elseif
• Ternary
 • $a = (isset($b)) : ‘mike’ ? ‘chris’;

                                    ...
Loops

• while
• do...while
• for
• break / continue

                       41
Errors



         42
Error Levels

• Compile Time
 • Errors occured during script compilation.
 • Script execution is halted


                ...
Error Levels

• Fatal Errors
 • Errors in syntax, etc.
 • Script execution is halted


                                44
Error Levels

• Recoverable Errors
 • Significant failures
 • Can still run in a safe way


                               ...
Error Levels

• Warnings
 • Recoverable errors.
 • Script execution continues


                                46
Error Levels

• Notices
 • Generally small errors. IE Undefined
    variables, etc.
 • Script execution continues


       ...
Error Reporting

• php.ini Directives
 • error_reporting
 • display_errors
 • log_errors

                        48
Handling Errors


• set_error_handler()



                        49
Questions?



             50
Useful Resources



                   51
Reading
• ZEND PHP 5 Certification Study Guide
  http://www.phparch.com/shop_product.php?itemid=135


• Programming PHP
  h...
Upcoming SlideShare
Loading in …5
×

Cfphp Zce 01 Basics

2,356 views

Published on

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

No Downloads
Views
Total views
2,356
On SlideShare
0
From Embeds
0
Number of Embeds
39
Actions
Shares
0
Downloads
43
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Cfphp Zce 01 Basics

  1. 1. CFPHP ZCE Training Session One: PHP Basics 1
  2. 2. In Today’s Episode • • PHP Files Summary • • Data Types Useful Resources • Variables & Constants • Operators • Control Structures • Errors 2
  3. 3. PHP Files 3
  4. 4. Scripts • Default Extension: .php • May be Overridden via Server Conf • Not Compiled, Interpreted • Thus, PHP is a text-processing language 4
  5. 5. Tags • Standard Tags • <?php ?> • Short Tags • <? ?> 5
  6. 6. Tags • Script Tags • <script language=“PHP”></script> • ASP Tags • <% %> • * Standard tags are the only recommendation 6
  7. 7. Newlines • Interpreter removes newlines at end of all scripts • This prevents “Headers Already Sent” errors 7
  8. 8. Statement Termination • Semicolons terminate PHP statements • function_call(); • $a = 123; • There are few exceptions • Control Structures • Last Line in PHP Script 8
  9. 9. Comments • // Inline Comment • # PERL Style Comment • /* C Style Multiline Comment */ 9
  10. 10. Whitespace • PHP = Loosely Typed • Whitespace is usually ignored • Exceptions: • Opening/Closing Tags • Keywords • Functions/Variable Names 10
  11. 11. Code Blocks • Defined by { Curly Braces } • Groups code into related blocks • Runs under specific circumstances • Functions • Conditionals • Loops 11
  12. 12. Language Constructs • Similar to Functions... different rules • if(), while(), for(), foreach(), function() • echo() vs print() • die(), exit() 12
  13. 13. Data Types 13
  14. 14. Data Types • 2 Main Categories • Scalar Values • Compound Values 14
  15. 15. Data Types • Numeric Types • Integer • Floating Point 15
  16. 16. Data Types • Integers • Sined values only • Defined Via • Decimal Notation • Octal Notation • Hexadecimal Notation 16
  17. 17. Data Types • Floating Point • AKA: Doubles, Floats • Defined Via: • Decimal Notation • Exponential Notation 17
  18. 18. Data Types • Strings • Junkdrawers for Binary Data (Usually Text) • Single Quotes = Simple Strings • Double Quotes = Complex Strings • Heredoc = Long Complex Strings 18
  19. 19. Data Types • Booleans • TRUE • FALSE 19
  20. 20. Data Types • Compound Data Types • Array • Object 20
  21. 21. Data Types • Other Data Types • null • Resources 21
  22. 22. Variables & Constants 22
  23. 23. Syntax • Defined by $ • Names may be alpha-numeric • May contain underscores • May NOT begin with a Number 23
  24. 24. Variable Variables • Variable Name becomes a variable • Generally not recommended • Code Difficult to read 24
  25. 25. Variable Existance • isset() 25
  26. 26. Constants • Similar to variables, but not • Values Stored once, may never be changed • Names are suggested to be in ALL_CAPS • define(), defined() 26
  27. 27. Operators 27
  28. 28. Arithmetic • Addition: + • Subtraction: - • Multiplication: * • Division: / • Modulus: % 28
  29. 29. Arithmetic • Increment Before: ++$a • Increment After: $a++ • Decrement Before: --$a • Decrement After: $a-- 29
  30. 30. String Concatenation • Period is only Concatenation Operator • Two Ways to Concatenate • $c = $a . $b; • $a .= $b; 30
  31. 31. Assignment • Simple Assignment • Arithmetic/Bitwise Assignment • $a += $b; • $a *= $b; 31
  32. 32. References • $a = 4; $b = $a; $a = 6; echo $b; 32
  33. 33. References • $a = 4; $b &= $a; $a = 6; echo $b; 33
  34. 34. References • Scalar Values and Arrays are always COPIED • Objects are Passed Via Reference 34
  35. 35. Comparison • Results of Comparison Operations are ALWAYS boolean • Equal == • Identical === • Not Equal != • Not Identical !== 35
  36. 36. Comparison • Less Than < • Greater Than > • Less Than or Equal To <= • Greater Than or Equal To >= 36
  37. 37. Logical • NOT ! • AND && / and • OR || / or • XOR 37
  38. 38. Other • Error Control Operator @ • @mysql_connect(‘foo’, ‘bar’, ‘beer’); • Backtick • $a = `ls -la`; 38
  39. 39. Control Structures 39
  40. 40. Conditionals • if • else • elseif • Ternary • $a = (isset($b)) : ‘mike’ ? ‘chris’; 40
  41. 41. Loops • while • do...while • for • break / continue 41
  42. 42. Errors 42
  43. 43. Error Levels • Compile Time • Errors occured during script compilation. • Script execution is halted 43
  44. 44. Error Levels • Fatal Errors • Errors in syntax, etc. • Script execution is halted 44
  45. 45. Error Levels • Recoverable Errors • Significant failures • Can still run in a safe way 45
  46. 46. Error Levels • Warnings • Recoverable errors. • Script execution continues 46
  47. 47. Error Levels • Notices • Generally small errors. IE Undefined variables, etc. • Script execution continues 47
  48. 48. Error Reporting • php.ini Directives • error_reporting • display_errors • log_errors 48
  49. 49. Handling Errors • set_error_handler() 49
  50. 50. Questions? 50
  51. 51. Useful Resources 51
  52. 52. Reading • ZEND PHP 5 Certification Study Guide http://www.phparch.com/shop_product.php?itemid=135 • Programming PHP http://en.wikibooks.org/wiki/PHP • Practical PHP Programming http://hudzilla.org/phpwiki/index.php?title=Main_Page • The PHP Manual http://us2.php.net/manual/en/ 52

×