Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Php basics

2,618 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Php basics

  1. 1. PHP BasicsNelson Ochieng
  2. 2. Instruction Separation• PHP requires instructions to be terminated with a semicolon at the end of each statement.• The closing tag of a block of PHP code automatically implies a semicolon;<?php echo This is a test;?><?php echo This is a test ?><?php echo We omitted the last closing tag;
  3. 3. Comments<?php echo This is a test; // This is a one-line c++ style comment /* This is a multi line comment yet another line of comment */ echo This is yet another test; echo One Final Test; # This is a one-line shell-style comment?>
  4. 4. Types• Some types that PHP supports.• Four scalar types: – boolean – integer – float (floating-point number, aka double) – string• Two compound types: – array – object• And finally three special types: – resource – NULL
  5. 5. <?php$a_bool = TRUE; // a boolean$a_str = "foo"; // a string$a_str2 = foo; // a string$an_int = 12; // an integerecho gettype($a_bool); // prints out: booleanecho gettype($a_str); // prints out: string// If this is an integer, increment it by fourif (is_int($an_int)) { $an_int += 4;}// If $a_bool is a string, print it out// (does not print out anything)if (is_string($a_bool)) { echo "String: $a_bool";}?>
  6. 6. Booleans• A boolean expression expresses a truth value. Can be TRUE or FALSE<?php$foo = True; // assign the value TRUE to $foo?><?php// == is an operator which tests// equality and returns a booleanif ($action == "show_version") { echo "The version is 1.23";}// this is not necessary...if ($show_separators == TRUE) { echo "<hr>n";}// ...because this can be used with exactly the same meaning:if ($show_separators) { echo "<hr>n";}
  7. 7. Integers• An integer is a number of the set Z = {..., -2, -1, 0, 1, 2, ...}.• Integers can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) notation, optionally preceded by a sign (- or +).• To use octal notation, precede the number with a 0 (zero). To use hexadecimal notation precede the number with 0x. To use binary notation precede the number with 0b.<?php$a = 1234; // decimal number$a = -123; // a negative number$a = 0123; // octal number (equivalent to 83 decimal)$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)?>
  8. 8. Strings• A string is series of characters, where a character is the same as a byte.• A string literal can be specified in four different ways: – single quoted – double quoted – heredoc syntax – nowdoc syntax (since PHP 5.3.0)
  9. 9. Single Quoted<?php echo this is a simple string; echo You can also have embedded newlines in strings this way as it is okay to do; // Outputs: Arnold once said: "Ill be back" echo Arnold once said: "Ill be back"; // Outputs: You deleted C:*.*? echo You deleted C:*.*?; // Outputs: You deleted C:*.*? echo You deleted C:*.*?; // Outputs: This will not expand: n a newline echo This will not expand: n a newline; // Outputs: Variables do not $expand $either echo Variables do not $expand $either; ?>
  10. 10. Double Quoted• The most important feature of double-quoted strings is the fact that variable names will be expanded.Heredoc• A third way to delimit strings is the heredoc syntax: <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.• The closing identifier must begin in the first column of the line. Also, the identifier must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.Nowdoc• A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<EOT. All the rules for heredoc identifiers also apply to nowdoc identifiers, especially those regarding the appearance of the closing identifier.
  11. 11. Arrays• An array in PHP is actually an ordered map.• A map is a type that associates values to keys.• This type is optimized for several different uses: – Array – list (vector) – hash table (an implementation of a map) – Dictionary – Collection – Stack – Queue • As array values can be other arrays, trees and multidimensional arrays are also possible.
  12. 12. Specifying an Array• An array can be created using the array() language construct.• It takes any number of comma-separated key => value pairs as arguments.array( key => value, key2 => value2, key3 => value3, ...)
  13. 13. Specifying an Array• As of PHP 5.4 you can also use the short array syntax, which replaces array() with [].<?php$array = array( "foo" => "bar", "bar" => "foo",);// as of PHP 5.4$array = [ "foo" => "bar", "bar" => "foo",];?>• The key can either be an integer or a string. The value can be of any type.
  14. 14. Accessing array elements with square bracket syntax• Array elements can be accessed using the array[key] syntax.<?php $array = array( "foo" => "bar", 42 => 24, "multi" => array( "dimensional" => array( "array" => "foo" ) ) ); var_dump($array["foo"]); var_dump($array[42]); var_dump($array["multi"]["dimensional"]["array"]); ?>
  15. 15. Creating/modifying with square bracket syntax• An existing array can be modified by explicitly setting values in it.• This is done by assigning values to the array, specifying the key in brackets. The key can also be omitted, resulting in an empty pair of brackets ([]).$arr[key] = value;$arr[] = value;// key may be an integer or string// value may be any value of any type• To change a certain value, assign a new value to that element using its key.• To remove a key/value pair, call the unset() function on it.
  16. 16. Objects• Object Initialization – To create a new object, use the new statement to instantiate a class:<?php class foo {     function do_foo()     {         echo "Doing foo.";      } } $bar = new foo; $bar->do_foo(); ?> 
  17. 17. Resources• A resource is a special variable, holding a reference to an external resource. Resources are created and used by special functions
  18. 18. Null• The special NULL value represents a variable with no value.

×