Presentation is loading. Please wait.

Presentation is loading. Please wait.

CST336, Dr. Krzysztof Pietroszek Week 2: PHP. 1.Introduction to PHP 2.Embed PHP code into an HTML web page 3.Generate (output HTML) web page using PHP.

Similar presentations


Presentation on theme: "CST336, Dr. Krzysztof Pietroszek Week 2: PHP. 1.Introduction to PHP 2.Embed PHP code into an HTML web page 3.Generate (output HTML) web page using PHP."— Presentation transcript:

1 CST336, Dr. Krzysztof Pietroszek Week 2: PHP

2 1.Introduction to PHP 2.Embed PHP code into an HTML web page 3.Generate (output HTML) web page using PHP. 4.Use PHP variables with different data types 5.Use loops and conditions 6.Define and use PHP functions 7.Generate random number and characters

3 Readings: Beginning PHP, Page 30-57 PHP Basics Training Video Tutorials - Sections A, Section B 1-8 and Section C Lecture notes Lab demo tutorial Complete Lab 2 and Assignment 2 individually and link it to your course homepage (from Assignment 1) Update your journal Students demo

4 PHP is a server-side scripting language (runs in the server, not in the browser) A PHP block of code is surrounded by: The PHP code is located usually within the tag: <?php //no space between the question mark and php echo "hello world!"; //a semicolon ends each line of code ?>

5 All variables MUST start with a "$" sign: $firstName The first character of a variable name MUST NOT BE a digit: $5abc - NO! PHP is a loosely-typed language (no need to declare variables' data types, a variable can be assigned different data types): $firstName = "John"; //first we assign a string to $firstName $firstName = 5; //then we assign a number to it. This is totally okay!

6 String values are surrounded by single or double quotation marks: $firstName = "Jane"; To join or concatenate two or more strings use a dot ". " $fullName = "Jane ". " Smith"; //Result = "Jane Smith"; Do not use the plus " + " sign (like in JavaScript)! $fullName = "Jane " + " Smith"; //Result = 0;

7 number_format() – Gives format to a number $n = 20943; number_format ($n); // 20,943 number_format ($n, 2); // 20,943.00 round() – Rounds a floating point number $n = round(1.6) ; // 2 rand(n, m) - Gives a random number between n and m $n = rand(5,15); // random number between 5 and 15

8 The most common control structures are conditions and loops. Conditions We use conditions to evaluate whether an expression is true, for instance, whether a number is greater than, smaller than, or equal to other. In PHP, we could use the following three statements to do conditions: 1)if/else if (expression) { //code to be executed if expression is true } else { //code to be executed if expression is false }

9 Loops As in any other programming language, loops allow repeating a set of statements multiple times. The most common loops in PHP are: For loop: for ($i=0; $i<=5; $i++) { echo $i. "\n"; //displays values from 0 to 5 }

10 Adding random numbers in PHP. We will create a PHP that generates nine random numbers using a loop. The random numbers will be displayed within a table. The program will identify whether each number is odd or even. The program will also calculate the sum and average of the random numbers.


Download ppt "CST336, Dr. Krzysztof Pietroszek Week 2: PHP. 1.Introduction to PHP 2.Embed PHP code into an HTML web page 3.Generate (output HTML) web page using PHP."

Similar presentations


Ads by Google