1 / 26

FP512 WEB PROGRAMMING 1

FP512 WEB PROGRAMMING 1. CHAPTER 3 PHP BASIC PROGRAM STRUCTURE. 3.1 Basic structure of PHP syntax and program (Part 1). PREPARED BY: PN. NUR SYUHADA BINTI MOHAMAD. Learning Outcomes. 1. Apply the basic structure of PHP syntax and program. 2. Create classes and objects in PHP environment.

jdibella
Download Presentation

FP512 WEB PROGRAMMING 1

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. FP512 WEB PROGRAMMING 1 CHAPTER 3 PHP BASIC PROGRAM STRUCTURE 3.1 Basic structure of PHP syntax and program (Part 1) PREPARED BY: PN. NUR SYUHADA BINTI MOHAMAD

  2. Learning Outcomes 1. Apply the basic structure of PHP syntax and program. 2. Create classes and objects in PHP environment. 3. Use functions in PHP environment. 4. Use arrays and strings in PHP environment. 5. Create PHP web form. 6. Process web form data. 7. Understand cookies and sessions.

  3. 3.1 Apply the basic structure of PHP syntax and program. - Identify PHP syntax.- Write statements and comments.- Declare variables in PHP.- Assign different data type of a variable.- Use different types of operator on variables.- Write constants in the PHP script.- Use Conditional Statements and Loop in PHP Environment

  4. PHP Syntax • PHP script always starts with <?php and ends with ?> • Shorthand-support, <? and end with ?> • The others tags that also can be use: <% %>, <? ?>,<?= ?> • Example, index.php: <html> <body> <?php echo "Hello World"; ?> </body> </html>

  5. Statement in PHP • PHP must end with a semicolon ( ; ) • Two basic statements to output text with PHP: – echo a language construct that has no return value and cannot be used in an expression. – print behaves like a function with its own return value (although it is a language construct), can be used in an expression.

  6. Comment in PHP • // to make a single-line comment • /* and */ to make a large comment block or multi line comment. <html> <body> <?php //This is a comment /* This is a comment block */ ?> </body> </html>

  7. PHP Variables • PHP variables are used to hold values or expressions. • Rules for PHP variable names: – Variables in PHP starts with a $ sign, followed by the name of the variable – The variable name must begin with a letter or the underscore character – A variable name can only contain alphanumeric characters and underscores (A-z, 0-9, and _ )

  8. – A variable name should not contain spaces . – Variable names are case sensitive (y and Y are two different variables) • Create a variable containing a string, and a variable containing a number: <?php $txt="Hello World!"; $x=16; ?>

  9. Local Scope Variables • A variable declared within a PHP function is local and can only be accessed within that function <?php $a = 5; // global scope function myTest() { echo $a; // local scope } myTest(); ?>

  10. Global Scope Variables • Any variable that is defined outside of any function • Accessed from any part of the script that is not inside a function Output: 15 <?php $a = 5; $b = 10; function myTest() { global $a, $b; $b = $a + $b; } myTest(); echo $b; ?> $GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];

  11. Operator • An operator is a symbol or series of symbols that, when used in conjunction with values, perform an action and usually produces a new value. • An operand is a value used in conjunction with an operator. There are usually two or more operands to one operator.

  12. Arithmetic operators

  13. The assignment operator (=) • Basic assignment operator in PHP is "=". • Example: The value of "$x = 5" is 5.

  14. Incrementing/Decrementing Operators

  15. Comparison Operators • Comparison between two values

  16. Logical Operators

  17. Concatenation operator • Concatenation operator (.) is used to put two string values together. For example: <?php $txt1=“Hai Amy!"; $txt2=“Have a nice day!"; echo $txt1 . " " . $txt2; ?> Output: Hai Amy! Have a nice day!

  18. Constants in PHP • A constant is an identifier (name) for a simple value. It’s function returns the value of constant. • Syntax: constant(constant) <?php //define a constant define("GREETING",“I love PHP"); echo constant("GREETING"); ?> Output: I love PHP

  19. Exercises

  20. Q1: Uses of Variable <?php echo "Twinkle, Twinkle little star. <br/>"; $twinkle="Twinkle"; $star="star"; echo "$twinkle, $twinkle little $star.<br/>"; $twinkle="Thunder"; $star="elephant"; echo "$twinkle, $twinkle little $star."; ?>

  21. Q1: Answer

  22. Q2: Arithmetic Operators <?php $num = 8; echo "Value is now $num.<br/>"; $num += 2; echo "Add 2. Value is now $num. <br/>"; $num -= 4; echo "Subtract 4. Value is now $num. <br/>"; $num *= 5; echo "Multiply by 5. Value is now $num. <br/>"; $num /= 3; echo "Divide by 3. Value is now $num. <br/>"; $num++; echo "Increment value by one. Value is now $num.<br/>"; $num--; echo "Decrement value by one. Value is now $num."; ?>

  23. Q2: Answer Value is now 8. Add 2. Value is now 10. Subtract 4. Value is now 6. Multiply by 5. Value is now 30. Divide by 3. Value is now 10. Increment value by one. Value is now 11. Decrement value by one. Value is now 10.

  24. Q3: Arithmetic & Assignment Operators <?php $x=10; $y=7; $result=$x+$y; echo "$x + $y = $result<br />"; $result=$x-$y; echo "$x - $y = $result<br />"; $result=$x*$y; echo "$x * $y = $result<br />"; $result=$x/$y; echo "$x / $y = $result<br />"; $result=$x%$y; echo "$x % $y = $result<br />"; ?>

  25. Q3: Answer 10 + 7 = 17 10 - 7 = 3 10 * 7 = 70 10 / 7 = 1.4285714285714 10 % 7 = 3

  26. to be continue…

More Related