Presentation is loading. Please wait.

Presentation is loading. Please wait.

Basics.

Similar presentations


Presentation on theme: "Basics."— Presentation transcript:

1 Basics

2 Comments in PHP Standard C, C++, and shell comment symbols
// C++ and Java-style comment # Shell-style comments /* C-style comments These can span multiple lines */

3 Variables in PHP PHP variables must begin with a “$” sign
Case-sensitive ($Foo != $foo != $fOo) Global and locally-scoped variables Global variables can be used anywhere Local variables restricted to a function or class Certain variable names reserved by PHP Form variables ($_POST, $_GET) File Upload($_FILES) Etc.

4 Variable usage <?php
$foo = 25; // Numerical variable $bar = “Hello”; // String variable $foo = ($foo * 7); // Multiplies foo by 7 $bar = ($bar * 7); // Invalid expression ?>

5 Echo The PHP command ‘echo’ is used to output the parameters passed to it The typical usage for this is to send data to the client’s web-browser Syntax void echo (string arg1 [, string argn...]) In practice, arguments are not passed in parentheses since echo is a language construct rather than an actual function

6 Echo example <?php $foo = 25; // Numerical variable $bar = “Hello”; // String variable echo $bar; // Outputs Hello echo $foo,$bar; // Outputs 25Hello echo “5x5=”,$foo; // Outputs 5x5=25 echo “5x5=$foo”; // Outputs 5x5=25 echo ‘5x5=$foo’; // Outputs 5x5=$foo ?> Notice how echo ‘5x5=$foo’ outputs $foo rather than replacing it with 25 Strings in single quotes (‘ ’) are not interpreted or evaluated by PHP This is true for both variables and character escape-sequences (such as “\n”)

7 The strlen() function is used to find the length of a string.
Using the strlen() function The strlen() function is used to find the length of a string. Let's find the length of a string "Hello world!": <?php echo strlen("Hello world!"); ?> The output of the code above will be: 12 The length of a string is often used in loops or other functions, when it is important to know when the string ends. (i.e. in a loop, we would want to stop the loop after the last character in the string)

8 Using the strpos() function
The strpos() function is used to search for a string or character within a string. If a match is found in the string, this function will return the position of the first match. If no match is found, it will return FALSE. Let's see if we can find the string "world" in our string: <?php echo strpos("Hello world!","world"); ?> The output of the code above will be: 6 As you see the position of the string "world" in our string is 6. Reason is that the first position in the string is 0, not 1.

9 Complete PHP String Reference
For a complete reference of all string functions, go to the W3C complete PHP String Reference. The reference contains a brief description and examples of use for each function!

10 Operations (Arithmetic)
<?php $a=15; $b=30; $total=$a+$b; Print $total; Print “<p><h1>$total</h1>”; // total is 45 ?> $a - $b // subtraction $a * $b // multiplication $a / $b // division $a += 5 // $a = $a+5 Also works for *= and /=

11 Operations (Others…) && (and), || (or), ! (not)
Logical: && (and), || (or), ! (not) Comparison: == (is equal to), != (not equal to), <,> ,<=,>= (lt, gt, lte, gte) Assignment: =, +=, -=, *=, /=, .=, %=

12 Concatenation Hello PHP Use a period to join strings into one.
$string1=“Hello”; $string2=“PHP”; $string3=$string1 . “ ” . $string2; Print $string3; ?> Hello PHP

13 Escaping the Character
If the string has a set of double quotation marks that must remain visible, use the \ [backslash] before the quotation marks to ignore and display them. <?php     $MyString = "This is an \"escaped\" string";     $MySingleString = 'This \'will\' work';      $MyNonVariable = "I have \$zilch in my pocket";     $MyNewline = "This ends with a line return\n";     $MyFile = "c:\\windows\\system32\\myfile.txt"; ?>

14 PHP Control Structures
Control Structures: Are the structures within a language that allow us to control the flow of execution through a program or script. Grouped into conditional (branching) structures (e.g. if/else) and repetition structures (e.g. while loops). Example if/else if/else statement: if ($foo == 0) { echo ‘The variable foo is equal to 0’; } else if (($foo > 0) && ($foo <= 5)) { echo ‘The variable foo is between 1 and 5’; else { echo ‘The variable foo is equal to ‘.$foo;

15 If ... Else... No THEN in PHP <?php If($user==“John”)
{ Print “Hello John.”; } Else Print “You are not John.”; ?> If (condition) { Statements; } Else Statement; No THEN in PHP

16 The Switch Statement <?php
If you want to select one of many blocks of code to be executed, use the switch statement. The switch statement is used to avoid long blocks of if..elseif..else code. <?php switch ($x) {case 1: echo "Number 1"; break; case 2: echo "Number 2"; case 3: echo "Number 3"; default: echo "No number between 1 and 3"; } ?> The Switch Statement

17 While Loops hello PHP. hello PHP. hello PHP. <?php
$count=0; While($count<3) { Print “hello PHP. ”; $count += 1; // $count = $count + 1; // or // $count++; ?> While (condition) { Statements; } hello PHP. hello PHP. hello PHP.

18 Date Display $datedisplay=date(“yyyy/m/d”); Print $datedisplay; # If the date is April 1st, 2009 # It would display as 2009/4/1 2009/4/1 $datedisplay=date(“l, F m, Y”); Print $datedisplay; # If the date is April 1st, 2009 # Wednesday, April 1, 2009 Wednesday, April 1, 2009

19 Month, Day & Date Format Symbols
Jan F January m 01 n 1 Day of Month d 01 J 1 Day of Week l Monday D Mon

20 echo “<h2>Hello, World</h2>”; ?>
First PHP script Save as sample.php: <!-- sample.php --> <html><body> <strong>Hello World!</strong><br /> <?php echo “<h2>Hello, World</h2>”; ?> $myvar = "Hello World"; echo $myvar; ?> </body></html>


Download ppt "Basics."

Similar presentations


Ads by Google