1 / 36

Chapter 1 Language Basic

Chapter 1 Language Basic. Chapter Goals. This chapter provides a whirlwind tour of the core PHP language, covering such basic topics as: Basic Rules data types Variables Operators flow control statements

jdurant
Download Presentation

Chapter 1 Language Basic

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 1 Language Basic

  2. Chapter Goals • This chapter provides a whirlwind tour of the core PHP language, covering such basic topics as: • Basic Rules • data types • Variables • Operators • flow control statements • We start with the basic units of a PHP program and build up your knowledge from there.

  3. Basic Rules • Case Sensitivity • The names of user-defined classes and functions, as well as built-in constructs and keywords such as echo, while, class, etc., are case-insensitive. Thus, these three lines are equivalent: echo("hello, world"); ECHO("hello, world"); EcHo("hello, world"); • Variables, on the other hand, are case-sensitive. That is, $name, $NAME, and $NaME are three different variables.

  4. statement is a collection of PHP code that does something. Here is a small sample of PHP statements : echo "Hello, world"; myfunc(42, “John"); $name = “smith"; if ($a == $b) { echo “Correct?"; } PHP uses semicolons to separate simple statements. Semicolon before the closing brace is optional <?php if ($a == $b) { echo "Rhyme? And Reason?"; } echo “Web Programming with PHP“ ?> Statements and Semicolons

  5. Whitespace and Line Break • In general, whitespace doesn't matter in a PHP program. • For example, this statement: myfunc(42, “John"); myfunc( 42 , “John“ );

  6. Comment • Comments give information to people who read your code, but they are ignored by PHP • For single line: // comment text or # comment text • $x = 17; // store 17 into the variable $x whereas this • For multiple line: /* comment text */ • <?php /* if ($a == $b) { echo "Rhyme? And Reason?"; } */ echo “Web Programming with PHP“ • ?>

  7. Variables • variable is a special container that you can define to "hold" a value. • PHP is a weakly typed language, the data type of a variable can change as we change the data that it contains . • Rules for Naming Variables: • Variable names should begin with a dollar($) symbol. • Variable names can begin with an underscore. • Variable names cannot begin with a numeric character. • Variable names must be relevant and self-explanatory. • Here are some examples of valid and invalid variable names: • $salary valid • $_firstname valid • sex invalid: doesn’t start with dollar sign($) • $12result invalid: starts with number; it doesn’t start with letter or underscore

  8. Variable Variables • You can reference the value of a variable whose name is stored in another variable. For example: <?php $first = “hello”; $$first = “World”; echo “the value of the first variable is $first”; echo ”the value of the second variable is $hello ”; ?> • After these two statement executes, the variable $first has the value "hello", and the variable $hello has the value “World”.

  9. Variable References • In PHP, references are how you create variable aliases. To make $black an alias for the variable $white, use: $black =& $white; $big_long_variable_name = "PHP"; $short =& $big_long_variable_name; $big_long_variable_name .= " rocks!"; print "\$short is $short\n"; print "Long is $big_long_variable_name\n";

  10. Variable Scope • The scope of a variable determines those parts of the program that can access it. • There are four types of variable scope in PHP: local, global, static, and function parameters.

  11. 1). Local scope • A variable declared in a function is local to that function. That is, it is visible only to code in that function function update_counter ( ) { $counter++; Echo $counter; } $counter = 10; update_counter( ); echo $counter; //It will print 10

  12. 2.) Global scope • Variables declared outside a function are global. That is, they can be accessed from any part of the program • To allow a function to access a global variable, you can use the global keyword inside the function to declare the variable within the function. function update_counter ( ) { global $counter; $counter++;} $counter = 10; update_counter( ); echo $counter; #It will print 11

  13. 3.)Static variables • A static variable retains its value between calls to a function but is visible only within that function. • You declare a variable static with the static keyword. function update_counter ( ) { static $counter = 0; $counter++; echo "Static counter is now $counter\n"; } $counter = 10; update_counter( ); update_counter( ); echo "Global counter is $counter\n";

  14. Data Types • PHP provides four primitive data types: integers, floating point numbers, strings, and booleans 1.) Integers • Integers are whole numbers • integer values range from -2,147,483,648 to +2,147,483,647 • PHP automatically converts larger values to floating point numbers if you happen to overflow the range. • Use the is_int( ) function (or its is_integer( ) alias) to test whether a value is an integer. • PHP will accept integer values using three mathematical bases: decimal (base 10), octal (base 8), and hexadecimal (base 16). • Example: $decimal=16; $hex=0x10; $octal=020;

  15. 2.) Floating Point Numbers • Floating point numbers represent decimal values. • a double can be between 1.7E-308 to 1.7E+308. • A double may be expressed either as a regular number with a decimal point or in scientific notation • example: $var=0.017; $var=17.0E-3. • Use the is_float( ) function (or its is_real( ) alias) to test whether a value is a floating point number 3.) Strings • A string is a sequence of characters. A string can be delimited by single quotes or double quotes. • Double-quoted strings are subject to variable substitution and escape sequence handling, while single quotes are not. • Use the is_string( ) function to test whether a value is a string

  16. <?php $my_int = 50; $string_one = "The value of the variable is $my_int<BR>"; $string_two = 'The value of the variable is $my_int<BR>'; echo $string_one; echo $string_two; ?> Escape Sequence

  17. Type Casting • Type casting uses the C-style syntax in which you put the type you want in brackets before the variable or expression. • example: $var = (int)"123abc";

  18. Operators and Expressions 1.) Assignment Operator • It consists of the single character =. • The assignment operator takes the value of its right-hand operand and assigns it to its left-hand operand: $name = “Norton"; 2.) Arithmetic Operators

  19. Operators and Expressions 3.)Concatenation Operator • The concatenation operator is represented by a single period(“.”): $name=“PHP”; echo “Norton"." University“; echo “your name is ”.$name; 4.) Combined Assignment Operators • A combined assignment operator consists of a standard operator symbol followed by an equals sign

  20. Operators and Expressions 5.)Comparison Operators • They return the Boolean value true if the test is successful, or false otherwise.

  21. Operators and Expressions 6.) Logical Operators • The logical operators test combinations of Booleans

  22. Constants • You must use PHP's built-in define() function to create a constant • To use the define() function, you must place the name of the constant and the value you want to give it within the call's parentheses. • These values must be separated by a comma <?php define( "CONSTANT_NAME", 42); Echo”The value is ”.CONSTANT_NAME; ?> • The define() function can accept a third Boolean argument that determines whether or not the constant name should be case-independent • By default, constants are case-dependent

  23. Conditional Statements 1.) The if Statement • The if statement evaluates an expression between parentheses if ( expression ) { // code to execute if the expression evaluates to true } <?php $mood = "happy"; if ( $mood == "happy" ) { print "Hooray, I'm in a good mood"; } ?>

  24. Conditional Statements 2.) Using the else Clause with the if Statement if ( expression ) { // code to execute if the expression evaluates to true } else { // code to execute in all other cases } <?php $mood = "sad"; if ( $mood == "happy" ) { print "Hooray, I'm in a good mood"; } else { print "Not happy but $mood"; } ?>

  25. Conditional Statements 3.) Using the elseif Clause with the if Statement • You can use an if...elseif...else construct to test multiple expressions before offering a default block of code if ( expression ) { // code to execute if the expression evaluates to true } elseif (another expression) { // code to execute if the previous expression failed // and this one evaluates to true } else { // code to execute in all other cases } NOTE:The elseif clause can also be written as two separate words (else if).

  26. Conditional Statements <?php $mood = "sad"; if ( $mood == "happy" ) { print "Hooray, I'm in a good mood"; } elseif ( $mood == "sad" ) { print "Awww. Don't be down!"; } else { print "Neither happy nor sad but $mood"; } ?>

  27. Conditional Statements 4.) The switch Statement switch ( expression ) { case result1: // execute this if expression results in result1 break; case result2: // execute this if expression results in result2 break; default: // execute this if no break statement // has been encountered hitherto }

  28. Conditional Statements <?php $mood = "sad"; switch ( $mood ) { case "happy": print "Hooray, I'm in a good mood"; break; case "sad": print "Awww. Don't be down!"; break; default: print "Neither happy nor sad but $mood"; } ?>

  29. Conditional Statements 5.) Using the ? Operator • The ? or ternary operator is similar to the if statement but returns a value derived from one of two expressions separated by a colon. ( expression )?returned_if_expression_is_true:returned_if_expression_is_false; <?php $mood = "sad"; $text = ($mood=="happy") ? "I'm in a good mood" : "Not happy but $mood"; print "$text"; ?>

  30. Looping Statements 1.) The while Statement while ( expression ) { // do something } <?php $counter = 1; while ( $counter <= 12 ) { print "$counter times 2 is ".($counter*2)."<br>"; $counter++; } ?>

  31. Looping Statements 2.) The do...while Statement • The essential difference between the two is that the code block is executed before the truth test and not after it do { // code to be executed } while ( expression ); <?php $num = 1; do { print "Execution number: $num<br>\n"; $num++; } while ( $num > 200 && $num < 400 ); ?>

  32. Looping Statements 3.) The for Statement for ( initialization expression; test expression; modification expression ) { // code to be executed } <?php for ( $counter=1; $counter<=12; $counter++ ) { print "$counter times 2 is ".($counter*2)."<br>"; } ?>

  33. Looping Statements 4.) Breaking Out of Loops with the break Statement • The break statement enables you to break out of a loop based on the results of additional tests <?php for ( $counter=-4; $counter <= 10; $counter++ ) { if ( $counter == 0 ) break; $temp = 4000/$counter; print "4000 divided by $counter is... $temp<br>"; } ?>

  34. Looping Statements 5.)Skipping an Iteration with the continue Statement • The continue statement ends execution of the current iteration but doesn't cause the loop as a whole to end. • Instead, the next iteration begins immediately. <?php for ( $counter=-4; $counter <= 10; $counter++ ) { if ( $counter == 0 ) continue; $temp = 4000/$counter; print "4000 divided by $counter is... $temp<br>"; } ?>

  35. Looping Statements 6.) Nesting Loops • Loop statements can contain other loop statements. <?php print "<table border=\"1\">\n"; for ( $y=1; $y<=6; $y++ ) { print "<tr>\n"; for ( $x=1; $x<=6; $x++ ) { print "\t<td>"; print ($x*$y); print "</td>\n"; } print "</tr>\n"; } print "</table>"; ?>

  36. The end of Chapter 1 Thanks for your paying attention

More Related