Difference between revisions of "PHP105"

From mi-linux
Jump to navigationJump to search
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
[[Main Page]] >> [[CP2228|Web Application Development]] >> [[PHP|Workbook]] >> Expressions
 +
 
== Expressions, Operators, and Variable Assignments ==
 
== Expressions, Operators, and Variable Assignments ==
  
Line 5: Line 7:
 
Type the following code as part of a web page.  Experiment with changing values and operations until you fully understand how to manipulate variables in this exercise.
 
Type the following code as part of a web page.  Experiment with changing values and operations until you fully understand how to manipulate variables in this exercise.
  
  <nowiki><?
+
  <nowiki><?php
   $num = 1
+
   $num = 1;
 
   echo "num is now: $num<BR>";
 
   echo "num is now: $num<BR>";
 
   $num = 4 + 9;
 
   $num = 4 + 9;
Line 58: Line 60:
  
 
* Write a PHP script which assigns the first three prime numbers to the variables $prime1, $prime2 and $prime3.
 
* Write a PHP script which assigns the first three prime numbers to the variables $prime1, $prime2 and $prime3.
* Write a calculation to determine the total (addition) of these three numbers
+
* Write a calculation to determine the sum (addition) of these three numbers
* Write a calculation to determine the sum (multiplication) of the three numbers
+
* Write a calculation to determine the product (multiplication) of the three numbers
 
* Suitable lines of code should output the results in this type of manner:
 
* Suitable lines of code should output the results in this type of manner:
 
  The total of <first number> + <second number> + <third number> = <answer>
 
  The total of <first number> + <second number> + <third number> = <answer>
Line 78: Line 80:
 
The rules of precedence can be confusing at first.  Therefore it is strongly suggested that you use parentheses, eg
 
The rules of precedence can be confusing at first.  Therefore it is strongly suggested that you use parentheses, eg
  
  <nowiki><?
+
  <nowiki><?php
 
   // this produces an answer of 10
 
   // this produces an answer of 10
 
   $num = (2 * 3) + 4;
 
   $num = (2 * 3) + 4;
Line 99: Line 101:
 
eg if a variable called $age contains the string value "23", and you want to calculate how many years there are before that person reaches 40, the code would use the second of the listed functions above, e.g.
 
eg if a variable called $age contains the string value "23", and you want to calculate how many years there are before that person reaches 40, the code would use the second of the listed functions above, e.g.
  
  <nowiki><?
+
  <nowiki><?php
 
   // assigns the string value to $age   
 
   // assigns the string value to $age   
 
   $age = "23";
 
   $age = "23";
Line 118: Line 120:
 
Type the following PHP code in to a web page.  Predict what you think the output will be, and insert appropriate 'print' or 'echo' statements to display the results on your page.  Be careful, the second three type conversions are not so obvious.
 
Type the following PHP code in to a web page.  Predict what you think the output will be, and insert appropriate 'print' or 'echo' statements to display the results on your page.  Be careful, the second three type conversions are not so obvious.
  
  <nowiki><?
+
  <nowiki><?php
 
   // $num is set as an integer = 115
 
   // $num is set as an integer = 115
 
   $num = "100" + 15;
 
   $num = "100" + 15;
Line 147: Line 149:
  
 
Create a number of variables, assigning them with your personal details, as though these had been entered on a form – name, various fields of your address, post code, phone number, etc.  Using a web page, and as much PHP as possible, output these details, using appropriate text formatting.
 
Create a number of variables, assigning them with your personal details, as though these had been entered on a form – name, various fields of your address, post code, phone number, etc.  Using a web page, and as much PHP as possible, output these details, using appropriate text formatting.
 +
$myname
 +
$myaddress
 +
$mypostcode
  
 
== Ready to move on? ==
 
== Ready to move on? ==
 
[[PHP106|PHP106 - Flow Control - Selection]]
 
[[PHP106|PHP106 - Flow Control - Selection]]

Latest revision as of 09:40, 14 November 2017

Main Page >> Web Application Development >> Workbook >> Expressions

Expressions, Operators, and Variable Assignments

Exercise 1

Type the following code as part of a web page. Experiment with changing values and operations until you fully understand how to manipulate variables in this exercise.

<?php
   $num = 1;
   echo "num is now: $num<BR>";
   $num = 4 + 9;
   echo "num is now: $num<BR>";
   /*
   ** all the following add one to the value of $num
   */
   $num = $num + 1;
   echo "num is now: $num<BR>";
   $num += 1;
   echo "num is now: $num<BR>";
   $num++;
   echo "num is now: $num<BR>";
   /*
   ** these subtract one from $num
   */
   $num = $num -1;
   echo "num is now: $num<BR>";
   $num -= 1;
   echo "num is now: $num<BR>";
   $num--;
   echo "num is now: $num<BR>";
   /*
   ** double the value of $num
   */
   $num = 23;
   echo "num is now: $num<BR>";
   $num = $num * 2;
   echo "num is now: $num<BR>";
   $num *= 2;
   echo "num is now: $num<BR>";
   /*
   ** halve the value of $num
   */
   $num = 100;
   echo "num is now: $num<BR>";
   $num = $num / 2;
   echo "num is now: $num<BR>";
   $num /= 2;
   echo "num is now: $num<BR>";
   /*
   ** A combination 
   */
   $num = 8;
   echo "num is now: $num<BR>";
   $num = (((($num + 4) * 2)  - 6) / 3);
   echo "num is now: $num<BR>";
 ?>

Exercise 2

  • Write a PHP script which assigns the first three prime numbers to the variables $prime1, $prime2 and $prime3.
  • Write a calculation to determine the sum (addition) of these three numbers
  • Write a calculation to determine the product (multiplication) of the three numbers
  • Suitable lines of code should output the results in this type of manner:
The total of <first number> + <second number> + <third number> = <answer>

PHP contains a library of mathematical functions which are available to you. These are well documented elsewhere, and you are encouraged to research this topic.

Operator Precedence

When a mixed expression is presented to the PHP interpreter, the order in which the calculation is to be performed must be determined - unless parentheses (brackets) clarify the intended order. Without parentheses the expression is evaluated according to the 'order of precedence' – which amongst other things states that multiplication and division occur before addition and subtraction.

For example, what is the value of $num when this expression has been evaluated:

$num = 2 * 3 + 4;

Is it 10 ? (2 times 3 is 6, plus 4 is 10). Is it 14 ? ( twice 7 – the 3 plus 4)

The rules of precedence can be confusing at first. Therefore it is strongly suggested that you use parentheses, eg

<?php
   // this produces an answer of 10
   $num = (2 * 3) + 4;
   echo $num."<BR>";
   //this produces an answer of 14
   $num = 2 * (3 + 4);
   echo $num."<BR>";
 ?>

Type Conversion

At times you may wish to convert a variable from one type to another. An example might be if someone's age is stored as "23". This is a string of characters. If you wanted to do any calculations with it, you will have to convert it in to an Integer.

PHP offers functions that do this for you, as well as some automatic type conversion.

  • A string variable = strval(a mixed variable)
  • An integer variable = intval(a mixed variable)
  • A float variable = floatval(a mixed variable)

eg if a variable called $age contains the string value "23", and you want to calculate how many years there are before that person reaches 40, the code would use the second of the listed functions above, e.g.

<?php
   // assigns the string value to $age  
   $age = "23";
   //convert the string to an Integer
   $int_age = intval($age);
   $years_to_go = 40 - $int_age;
   echo "The years to go before forty is $years_to_go";
 ?>

This produces:

The years to go before forty is 17

Automatic Type Conversion

This occurs when variables of two or more types are mixed in an expression, with one of the variables being used as though it were of a different type. Under these circumstances, PHP will automatically convert the variable. The conversion follows set built-in rules, therefore it is best to ensure that you have clarity in all expressions you write, or that you write code to do the conversion for you.

Type the following PHP code in to a web page. Predict what you think the output will be, and insert appropriate 'print' or 'echo' statements to display the results on your page. Be careful, the second three type conversions are not so obvious.

<?php
   // $num is set as an integer = 115
   $num = "100" + 15;
   // $num is set as a float = 115.5
   $num = "100" + 15.5;
   // $title is set as a string = "39 steps"
   $title = 39 . " steps";
   // $num is set as an integer = 39
   $num = 39 + " steps";
   // $num is an integer = 42
   $num = 40 + "2 blind mice";
   // $num is a float, but what does it mean ?
   $num = "test" * 4 + 3.14159;
 ?>

Ensure you put some line breaks in to your code – to improve appearance.

ONCE YOU'VE PUT SOME ECHO OR PRINT STATEMENTS IN, THE CODE SHOULD DISPLAY SOMETHING LIKE:

115
115.5
39 steps
39
42
3.14159

Exercise 3

Create a number of variables, assigning them with your personal details, as though these had been entered on a form – name, various fields of your address, post code, phone number, etc. Using a web page, and as much PHP as possible, output these details, using appropriate text formatting. $myname $myaddress $mypostcode

Ready to move on?

PHP106 - Flow Control - Selection