Php Chapter 1 Training

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    3 Favorites

    Php Chapter 1 Training - Presentation Transcript

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

    + Chris ChubbChris Chubb, 9 months ago

    custom

    692 views, 3 favs, 1 embeds more stats

    PHP ZEND Certification Training
    Chapter 1 Fundament more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 692
      • 691 on SlideShare
      • 1 from embeds
    • Comments 0
    • Favorites 3
    • Downloads 32
    Most viewed embeds
    • 1 views on http://cviserver.ddns.comp.nus.edu.sg

    more

    All embeds
    • 1 views on http://cviserver.ddns.comp.nus.edu.sg

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories