PHP Basics Orlando PHP Meetup Zend Certification Training January 2008
Anatomy of a Web Request <ul><li>What happens when you request index.php? </li></ul><ul><li>Web server (Apache) loads hand...
PHP Syntax <ul><li>Tags (Must be matched pairs) </li></ul><ul><ul><li><?php code ?> - Recommended </li></ul></ul><ul><ul><...
Comments <ul><li>// Single line comment - Preferred </li></ul><ul><li># Single line comment – Valid but deprecated </li></...
Variables <ul><li>All variables start with dollar sign: $ </li></ul><ul><ul><li>$scalar – Holds a single value </li></ul><...
Language Constructs <ul><li>Code Blocks – Wrapped in {} </li></ul><ul><ul><ul><li>{ // Some comments f(); // a function ca...
Data Types <ul><li>Scalar Types </li></ul><ul><ul><li>boolean - A value that can only either be false (value == 0 or value...
Data Types (2) <ul><li>Compound Types </li></ul><ul><ul><li>Array – An ordered hash of key => value pairs </li></ul></ul><...
Data Type Conversion <ul><li>Loosely typed </li></ul><ul><ul><li>A single variable can contain different types over it’s l...
Variable Naming <ul><li>Must start with a dollar sign: $ </li></ul><ul><li>Then a character (a-zA-z) or underscore </li></...
Variable Scope <ul><li>Function </li></ul><ul><ul><li>Defined when first referenced (var) or assigned to. </li></ul></ul><...
Constants <ul><li>define(‘CONSTANT’, ‘scalarvalue’); </li></ul><ul><li>echo CONSTANT; //No $ or single quote </li></ul><ul...
Operators 1 <ul><li>Assignment Operators  </li></ul><ul><ul><li>assigning data to variables ($a = 1; $b = $c = 2;) </li></...
Operators 2 <ul><li>Bitwise Operators  </li></ul><ul><ul><li>Manipulating bits using boolean math ($a = 2 & 4;) </li></ul>...
Operator Precedence & Associativity instanceof non-associative , left or left xor left and left = += -= *= /= .= %= &= |= ...
Control Structures <ul><li>If – Then – Else  </li></ul><ul><ul><li>if (expression1) {   // True expressions } elseif (expr...
Switch statement <ul><li>Does not need to evaluate on each comparison </li></ul><ul><ul><li>$a = 0; switch ($a) { // In th...
Iteration Constructs <ul><li>While (pre-comparison) </li></ul><ul><ul><li>$i = 0; while ($i < 10) {   echo $i . PHP_EOL;  ...
for() and foreach() <ul><li>for(init ; compare ; increment) {} </li></ul><ul><ul><li>for ($i = 0; $i < 10;$i++) {   echo $...
Breaking Out: break [n] <ul><li>Exits the current loop ( for, foreach, while, do-while or switch but  NOT if) and optional...
Continue <ul><li>Skips rest of loop and restarts </li></ul><ul><ul><li>for ($i = 0; $i < 10; $i++) {   if ($i > 3 && $i < ...
Errors and Error Management <ul><li>Types of errors: </li></ul><ul><ul><li>Compile-time errors </li></ul></ul><ul><ul><ul>...
Error Reporting <ul><li>Set via INI configurations </li></ul><ul><ul><li>error_reporting=E_ALL & ~E_NOTICE </li></ul></ul>...
Handling Errors <ul><li>Global error handling function </li></ul><ul><ul><li>$oldErrorHandler = ’’; //Stores name of old f...
Summary <ul><li>Language fundamentals are building blocks of programming. </li></ul><ul><ul><li>Build a strong foundation ...
Homework <ul><li>Write the classic “Hello World” application.  </li></ul><ul><ul><li>Build an index.php that prints out “H...
Upcoming SlideShare
Loading in …5
×

Php Chapter 1 Training

3,592 views

Published on

PHP ZEND Certification Training
Chapter 1 Fundamentals

Published in: Technology
1 Comment
4 Likes
Statistics
Notes
  • IT Pathshala, Noida-Sector-63, offers Guaranteed JOB with PHP Training Programs.

    PAY US ONLY IF YOU GET A JOB!

    CALL US - +91-9871350999 or +91-8800394970

    Technology Centre Address: H-110, Sector-63, Noida (UP)

    Program Takeaways:

    - GUARANTEED JOB in your technology domain
    - Live Major Project development
    - Experience Letter on Training Completion
    - Certification on Training Completion
    - Project Source Code available in CD

    Program Highlights:

    - 100% Guaranteed Job – Don’t pay us if you don’t get a JOB!
    - Industry Ready Courses
    - LIVE International Project Experience
    - Exposure to corporate culture at our IT services facility
    - Networking & Interaction with Industry Experts
    - Training Provided by a reputed IT Company
    - Live Project Source Code made available in CD
    - Technology certification and experience letter at program completion
    - Online Study Content

    CALL US - +91-9871350999 or +91-8800394970

    Our website: www.itpathshala.com
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
3,592
On SlideShare
0
From Embeds
0
Number of Embeds
15
Actions
Shares
0
Downloads
111
Comments
1
Likes
4
Embeds 0
No embeds

No notes for slide

Php Chapter 1 Training

  1. 1. PHP Basics Orlando PHP Meetup Zend Certification Training January 2008
  2. 2. Anatomy of a Web Request <ul><li>What happens when you request index.php? </li></ul><ul><li>Web server (Apache) loads handler, php application and provides URL and POST data, if any. </li></ul><ul><li>PHP: </li></ul><ul><ul><li>Parse page to split code from html </li></ul></ul><ul><ul><li>Compile code </li></ul></ul><ul><ul><li>Execute code </li></ul></ul><ul><ul><li>Merges code output with html </li></ul></ul><ul><ul><li>Stream back to Apache, which forwards to the users browser. </li></ul></ul>
  3. 3. PHP Syntax <ul><li>Tags (Must be matched pairs) </li></ul><ul><ul><li><?php code ?> - Recommended </li></ul></ul><ul><ul><li><? ?> - Frequently used, <?= ?> (auto echo) </li></ul></ul><ul><ul><li><% %>, <script language=“php”> </script> </li></ul></ul><ul><li>Whitespace </li></ul><ul><ul><li>Inside script tags: Ignored </li></ul></ul><ul><ul><ul><li>Just don’t break any <?php tags or function names. </li></ul></ul></ul><ul><ul><li>Outside script tags: Sent to output stream exactly </li></ul></ul><ul><li>Case Sensitive </li></ul><ul><ul><li>$This != $this != $THIS </li></ul></ul>
  4. 4. Comments <ul><li>// Single line comment - Preferred </li></ul><ul><li># Single line comment – Valid but deprecated </li></ul><ul><li>/* Multi-line comment */ </li></ul><ul><li>/** * API Documentation Example * * @param string $bar */ function foo($bar) { } </li></ul>
  5. 5. Variables <ul><li>All variables start with dollar sign: $ </li></ul><ul><ul><li>$scalar – Holds a single value </li></ul></ul><ul><ul><li>$array[$index] – Single element of an array </li></ul></ul><ul><ul><li>$object->method() – Object and method </li></ul></ul><ul><ul><li>$object->property – Object and property </li></ul></ul><ul><li>Variable variables: $$variablename </li></ul><ul><ul><li>$variablename = ‘size’; </li></ul></ul><ul><ul><li>$$variablename == $size </li></ul></ul><ul><ul><li>(Use sparingly, can drive mortal programmers insane) </li></ul></ul>
  6. 6. Language Constructs <ul><li>Code Blocks – Wrapped in {} </li></ul><ul><ul><ul><li>{ // Some comments f(); // a function call } </li></ul></ul></ul><ul><li>Statements terminated with semicolon. </li></ul><ul><ul><li>Single or multiline: </li></ul></ul><ul><ul><ul><li>echo(‘this is printed’ . ‘on a single ‘ . ‘line.’); </li></ul></ul></ul>
  7. 7. Data Types <ul><li>Scalar Types </li></ul><ul><ul><li>boolean - A value that can only either be false (value == 0 or value = ‘’ or value ==‘0’) or true (any other value) </li></ul></ul><ul><ul><li>int - A signed numeric integer value </li></ul></ul><ul><ul><ul><li>Decimal: 1234567890 </li></ul></ul></ul><ul><ul><ul><li>Octal: 01234567 (Leading zero) </li></ul></ul></ul><ul><ul><ul><li>Hex: 0x1234567890ABCDEF </li></ul></ul></ul><ul><ul><li>float - A signed floating-point value </li></ul></ul><ul><ul><ul><li>Decimal: 12345.6789 </li></ul></ul></ul><ul><ul><ul><li>Exponential: 123.45e67 or 123.45E67 </li></ul></ul></ul><ul><ul><li>string - A collection of character or binary data </li></ul></ul>
  8. 8. Data Types (2) <ul><li>Compound Types </li></ul><ul><ul><li>Array – An ordered hash of key => value pairs </li></ul></ul><ul><ul><ul><li>Key evaluates to integer or string </li></ul></ul></ul><ul><ul><ul><li>Value may be any data type. </li></ul></ul></ul><ul><ul><li>Object – Containers of data and code. </li></ul></ul><ul><li>Special Types </li></ul><ul><ul><li>Resource – Handle to a file, database or connection </li></ul></ul><ul><ul><li>NULL – A special value for an uninitialized variable </li></ul></ul>
  9. 9. Data Type Conversion <ul><li>Loosely typed </li></ul><ul><ul><li>A single variable can contain different types over it’s lifespan </li></ul></ul><ul><li>Mostly transparent, but can be forced </li></ul><ul><ul><li>$var = (int) (‘123’ + ‘456’) == 123456 </li></ul></ul><ul><ul><li>$var = (int) ‘123’ + ‘456’ == 579 (Early bind) </li></ul></ul><ul><ul><li>Cannot convert TO a resource or class. </li></ul></ul><ul><li>When converting to a boolean: </li></ul><ul><ul><li>false == 0, ‘0’, ‘’, null, unset() </li></ul></ul><ul><ul><li>true == (! false) (Caution: ’00’ or ‘ ‘ == true) </li></ul></ul>
  10. 10. Variable Naming <ul><li>Must start with a dollar sign: $ </li></ul><ul><li>Then a character (a-zA-z) or underscore </li></ul><ul><li>May contain numbers (not first character) </li></ul><ul><ul><li>Except variable variables </li></ul></ul><ul><ul><ul><li>$var = ‘123’ </li></ul></ul></ul><ul><ul><ul><li>$$var = ‘value’ </li></ul></ul></ul><ul><ul><ul><li>echo ${‘123’}; //outputs ‘value’ </li></ul></ul></ul><ul><li>No punctuation or special characters </li></ul><ul><ul><li>Valid: $value, $value123, $_val </li></ul></ul><ul><ul><li>Not Valid: $1value, $value.two, $value@home </li></ul></ul>
  11. 11. Variable Scope <ul><li>Function </li></ul><ul><ul><li>Defined when first referenced (var) or assigned to. </li></ul></ul><ul><ul><li>Not inherited from the call stack. </li></ul></ul><ul><ul><li>Disposed when function exits </li></ul></ul><ul><li>Global </li></ul><ul><ul><li>Defined outside a function </li></ul></ul><ul><ul><li>Inherit into a function with global() or $GLOBALS[‘varname’] </li></ul></ul><ul><ul><li>Limit use to improve maintainability </li></ul></ul><ul><li>Class </li></ul><ul><ul><li>Class properties are visible within the class via $this->varname </li></ul></ul>
  12. 12. Constants <ul><li>define(‘CONSTANT’, ‘scalarvalue’); </li></ul><ul><li>echo CONSTANT; //No $ or single quote </li></ul><ul><li>Immutable, scopeless, ONLY scalar values (int, float, boolean, string) </li></ul>
  13. 13. Operators 1 <ul><li>Assignment Operators </li></ul><ul><ul><li>assigning data to variables ($a = 1; $b = $c = 2;) </li></ul></ul><ul><ul><li>Value: $b = $a Reference: $b = &$a; (makes copy) </li></ul></ul><ul><li>Arithmetic Operators </li></ul><ul><ul><li>performing basic math functions ($a = $b + $c;) </li></ul></ul><ul><li>String Operators </li></ul><ul><ul><li>joining two or more strings ($a = ‘abc’ . ‘def’;) </li></ul></ul><ul><li>Comparison Operators </li></ul><ul><ul><li>comparing two pieces of data ($boolean = $a or $b;) </li></ul></ul><ul><li>Logical Operators </li></ul><ul><ul><li>performing logical operations on Boolean values </li></ul></ul>
  14. 14. Operators 2 <ul><li>Bitwise Operators </li></ul><ul><ul><li>Manipulating bits using boolean math ($a = 2 & 4;) </li></ul></ul><ul><li>Error Control Operators </li></ul><ul><ul><li>Suppressing errors ($handle = @fopen();) </li></ul></ul><ul><li>Execution Operators </li></ul><ul><ul><li>Executing system commands ($a = `ls –la`;) </li></ul></ul><ul><li>Incrementing/Decrementing Operators </li></ul><ul><ul><li>Inc. and dec. numerical values ($a += 1; $a++; ++$a;) </li></ul></ul><ul><li>Type Operators </li></ul><ul><ul><li>Identifying Objects </li></ul></ul>
  15. 15. Operator Precedence & Associativity instanceof non-associative , left or left xor left and left = += -= *= /= .= %= &= |= ˆ= <<= >>= right ? : left || left && left | left * left & left == != === !== non-associative < <= > >= non-associative << >> left + - . left * / % left ! Right ˜ - (int) (float) (string) (array) (object) @ non-associative ++ - non-associative [ left Operator Associativity
  16. 16. Control Structures <ul><li>If – Then – Else </li></ul><ul><ul><li>if (expression1) { // True expressions } elseif (expression2) { // Optional space between else and if } else { // Nothing else matches } </li></ul></ul><ul><ul><li>($a == $b) ? $truevalue : $falsevalue; </li></ul></ul>
  17. 17. Switch statement <ul><li>Does not need to evaluate on each comparison </li></ul><ul><ul><li>$a = 0; switch ($a) { // In this case, $a is the expression case true: // Compare to true // Evaluates to false break; case 0: // Compare to 0 // Evaluates to true break; default: // Will only be executed if no other conditions are met break; } </li></ul></ul>
  18. 18. Iteration Constructs <ul><li>While (pre-comparison) </li></ul><ul><ul><li>$i = 0; while ($i < 10) { echo $i . PHP_EOL; $i++; } </li></ul></ul><ul><li>Do (post comparison) </li></ul><ul><ul><li>$i = 0; do { echo $i . PHP_EOL; $i++; } while ($i < 10); </li></ul></ul>
  19. 19. for() and foreach() <ul><li>for(init ; compare ; increment) {} </li></ul><ul><ul><li>for ($i = 0; $i < 10;$i++) { echo $i . PHP_EOL; } </li></ul></ul><ul><li>foreach ($array as $element) </li></ul><ul><ul><li>$arr = array (‘one’, ‘two’, ‘three’); foreach ($arr as $item){ echo $item . PHP_EOL; } </li></ul></ul><ul><li>foreach ($assoc_array as $key => $item) </li></ul><ul><ul><li>$arr = array (‘one’ => ‘uno’, ‘two’ => ‘dos’); foreach ($arr as $english => $spanish) { echo “$english means $spanish ”; } </li></ul></ul>
  20. 20. Breaking Out: break [n] <ul><li>Exits the current loop ( for, foreach, while, do-while or switch but NOT if) and optionally parents </li></ul><ul><li>$i = 0; while (true) { if ($i == 10) { break; } echo $i . PHP_EOL; $i++; } </li></ul><ul><li>for ($i = 0; $i < 10; $i++) { for ($j = 0; $j < 3; $j++) { if (($j + $i) % 5 == 0) { break 2; // Exit from this loop and the next one. } } } //break continues here </li></ul>
  21. 21. Continue <ul><li>Skips rest of loop and restarts </li></ul><ul><ul><li>for ($i = 0; $i < 10; $i++) { if ($i > 3 && $i < 6) { continue; } echo $i . PHP_EOL; } </li></ul></ul><ul><ul><li>Can also take an optional parameter to restart optional parents. </li></ul></ul>
  22. 22. Errors and Error Management <ul><li>Types of errors: </li></ul><ul><ul><li>Compile-time errors </li></ul></ul><ul><ul><ul><li>Errors detected by the parser while it is compiling a script. Cannot be trapped from within the script itself. </li></ul></ul></ul><ul><ul><li>Fatal errors </li></ul></ul><ul><ul><ul><li>Errors that halt the execution of a script. Cannot be trapped. </li></ul></ul></ul><ul><ul><li>Recoverable errors </li></ul></ul><ul><ul><ul><li>Errors that represent significant failures, but can still be handled in a safe way. </li></ul></ul></ul><ul><ul><li>Warnings </li></ul></ul><ul><ul><ul><li>Recoverable errors that indicate a run-time fault. Do not halt the execution of the script. </li></ul></ul></ul><ul><ul><li>Notices </li></ul></ul><ul><ul><ul><li>Indicate that an error condition occurred, but is not necessarily significant. Do not halt the execution of the script. </li></ul></ul></ul>
  23. 23. Error Reporting <ul><li>Set via INI configurations </li></ul><ul><ul><li>error_reporting=E_ALL & ~E_NOTICE </li></ul></ul><ul><ul><li>From code: error_reporting(E_ALL & ~E_NOTICE) </li></ul></ul><ul><ul><li>display_errors = on #Show in browser </li></ul></ul><ul><ul><li>log_errors = on # Write to log file or web server log </li></ul></ul>
  24. 24. Handling Errors <ul><li>Global error handling function </li></ul><ul><ul><li>$oldErrorHandler = ’’; //Stores name of old function function myErrorHandler($errNo, $errStr, $errFile, $errLine, $errContext) { global $oldErrorHandler; logToFile(&quot;Error $errStr in $errFile at line $errLine&quot;); // Call the old error handler if ($oldErrorHandler) { $oldErrorHandler ($errNo, $errStr, $errFile, $errLine, $errContext); } } //Set up a new error handler function, returns the old handler function name $oldErrorHandler = set_error_handler(’myErrorHandler’); </li></ul></ul><ul><li>Ignore & Check </li></ul><ul><ul><li>$return = @function_call($params); //Internal only if ($return === FALSE) { //Handle Error } </li></ul></ul>
  25. 25. Summary <ul><li>Language fundamentals are building blocks of programming. </li></ul><ul><ul><li>Build a strong foundation and your architecture will follow. </li></ul></ul><ul><ul><li>Be clear, concise and always explain why when writing code. </li></ul></ul><ul><ul><li>Pick a standard and stick with it. </li></ul></ul><ul><ul><li>Don’t be afraid to read and reread the documentation </li></ul></ul><ul><ul><ul><li>http://www.php.net/manual/ </li></ul></ul></ul>
  26. 26. Homework <ul><li>Write the classic “Hello World” application. </li></ul><ul><ul><li>Build an index.php that prints out “Hello [[name]]” 5 times in the middle of a page. </li></ul></ul><ul><ul><li>Page needs to be fully formed html, <html> through </html>. </li></ul></ul><ul><ul><li>[[Name]] should be collected from the $_REQUEST[‘name’] variable. </li></ul></ul><ul><ul><li>If the [[name]] is less than 5 characters, it should be in all capitals. Otherwise, print it out as received. </li></ul></ul>

×