260 likes | 273 Views
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.
E N D
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. 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.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
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>
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.
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>
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 _ )
– 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; ?>
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(); ?>
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'];
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.
The assignment operator (=) • Basic assignment operator in PHP is "=". • Example: The value of "$x = 5" is 5.
Comparison Operators • Comparison between two values
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!
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
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."; ?>
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."; ?>
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.
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 />"; ?>
Q3: Answer 10 + 7 = 17 10 - 7 = 3 10 * 7 = 70 10 / 7 = 1.4285714285714 10 % 7 = 3