Difference between revisions of "PHP104"

From mi-linux
Jump to navigationJump to search
 
(10 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
[[Main Page]] >> [[CP2228|Web Application Development]] >> [[PHP|Workbook]] >> PHP Variables
 +
 
== PHP Variables & Constants - Basics ==
 
== PHP Variables & Constants - Basics ==
  
Line 7: Line 9:
 
This process of implicitly defining a variable means that if the value is changed, it can be implicitly re-defined.  The following line of code shows a variable called $num, and assigns it a value of 17.  This implicitly defines it as an integer (whole number).
 
This process of implicitly defining a variable means that if the value is changed, it can be implicitly re-defined.  The following line of code shows a variable called $num, and assigns it a value of 17.  This implicitly defines it as an integer (whole number).
  
  <?
+
  <?php
   $num=17;
+
   $num = 17;
 
  ?>
 
  ?>
  
 
To re-define this variable, so it contains text (a string of characters), we use the following code:
 
To re-define this variable, so it contains text (a string of characters), we use the following code:
 
   
 
   
  <?
+
  <?php
   $num="University of Wolverhampton";
+
   $num = "University of Wolverhampton";
 
  ?>
 
  ?>
  
Line 21: Line 23:
 
Look at the following example which uses both a string and an integer variable.
 
Look at the following example which uses both a string and an integer variable.
  
  <?
+
  <?php
 
   $students = 212;
 
   $students = 212;
   $module = "CP1082";
+
   $module = "5cs023";
 
   echo "The class $module has $students enrolled on it";
 
   echo "The class $module has $students enrolled on it";
 
  ?>
 
  ?>
Line 29: Line 31:
 
This produces:
 
This produces:
  
  The class CP1082 has 212 enrolled on it
+
  The class 5cs023has 212 enrolled on it
  
 
'''NOTE:''' - a common error is to use an undefined variable in an expression.
 
'''NOTE:''' - a common error is to use an undefined variable in an expression.
Line 35: Line 37:
 
== Types of Variable ==
 
== Types of Variable ==
  
There are four type of scalar (can contain a single value at any given time) variable supported by PHP. These are:
+
There are four type of scalar (can contain a single value at any given time) variable supported by PHP. These are:
  
 
* Boolean
 
* Boolean
Line 46: Line 48:
 
Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.  The underscore is used instead of a space (space in variables names is not permitted in PHP).
 
Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.  The underscore is used instead of a space (space in variables names is not permitted in PHP).
  
For example, if you chose the variable name '$my age' it would not be permitted.  The variable '$my_name' would be.
+
For example, if you chose the variable name '$my age' it would not be permitted.  The variable '$my_age' would be.
  
 
=== Boolean ===
 
=== Boolean ===
Line 56: Line 58:
 
A whole number, sometimes known as a counting number.  Note, these can be positive or negative, e.g. -37, 5, 4732, -162, 0, 56
 
A whole number, sometimes known as a counting number.  Note, these can be positive or negative, e.g. -37, 5, 4732, -162, 0, 56
  
  <?
+
  <?php
 
   // This is an integer
 
   // This is an integer
 
   $distance = 42;
 
   $distance = 42;
Line 64: Line 66:
 
A number which has a fractional part.  Note this may be a decimal, or represented in exponential notation (scientific format).
 
A number which has a fractional part.  Note this may be a decimal, or represented in exponential notation (scientific format).
  
  <?
+
  <?php
 
   // This is a float
 
   // This is a float
 
   $distance = 42.0;
 
   $distance = 42.0;
Line 76: Line 78:
  
 
We have already seen numerous examples of string variables on previous pages.  A string can contain a single character, or numerous characters.
 
We have already seen numerous examples of string variables on previous pages.  A string can contain a single character, or numerous characters.
  <?
+
  <?php
   $initial="B";
+
   $initial = "A";
   $surname="Riordan";
+
   $surname = "Bergeret";
 
   echo $initial." ".$surname;
 
   echo $initial." ".$surname;
 
  ?>
 
  ?>
Line 86: Line 88:
 
If variables are values which may 'vary' during the life of a script, then a constant is a value which remains 'constant' (does not vary) during the life of that script.  Therefore, it is assigned a value which is used throughout the script.  Examples would be the number of hours in a day, the number of students on a course, the rate of VAT etc.   
 
If variables are values which may 'vary' during the life of a script, then a constant is a value which remains 'constant' (does not vary) during the life of that script.  Therefore, it is assigned a value which is used throughout the script.  Examples would be the number of hours in a day, the number of students on a course, the rate of VAT etc.   
  
  <?
+
  <?php
 
   define ("PI", 3.142);
 
   define ("PI", 3.142);
 
   // output the value of twice PI
 
   // output the value of twice PI

Latest revision as of 10:22, 20 September 2023

Main Page >> Web Application Development >> Workbook >> PHP Variables

PHP Variables & Constants - Basics

A variable is a place in the computer’s memory where you can store some data. The stored value can change, and is often as the result of some calculation or processing having taken place.

A variable in PHP is identified by the first character being a dollar sign, ‘$’. Variables do not have to be declared before using them, and they have no value until assigned one. The process of assigning a value to the variable also usually defines what type of variable it is, e.g. text or numeric.

This process of implicitly defining a variable means that if the value is changed, it can be implicitly re-defined. The following line of code shows a variable called $num, and assigns it a value of 17. This implicitly defines it as an integer (whole number).

<?php
  $num = 17;
?>

To re-define this variable, so it contains text (a string of characters), we use the following code:

<?php
  $num = "University of Wolverhampton";
?>

Note, that this would not be a meaningful name for this variable. Perhaps a more meaningful name might be $university. The process of re-defining variables can both be flexible and dangerous, if not used with some care.

Look at the following example which uses both a string and an integer variable.

<?php
  $students = 212;
  $module = "5cs023";
  echo "The class $module has $students enrolled on it";
?>

This produces:

The class 5cs023has 212 enrolled on it

NOTE: - a common error is to use an undefined variable in an expression.

Types of Variable

There are four type of scalar (can contain a single value at any given time) variable supported by PHP. These are:

  • Boolean
  • Float
  • Integer
  • String

PHP also supports compound types (array or object). These can contain a number of scalar values, such as the days of the week, the scores of students in an exam etc.

Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. The underscore is used instead of a space (space in variables names is not permitted in PHP).

For example, if you chose the variable name '$my age' it would not be permitted. The variable '$my_age' would be.

Boolean

These are very simple variables, in that they can only hold the values true or false. They are often used to determine what action should be taken - according to their value.

Integer

A whole number, sometimes known as a counting number. Note, these can be positive or negative, e.g. -37, 5, 4732, -162, 0, 56

<?php
  // This is an integer
  $distance = 42;
?>

Float

A number which has a fractional part. Note this may be a decimal, or represented in exponential notation (scientific format).

<?php
  // This is a float
  $distance = 42.0;
  // This is a float equal to 1120
  $num = 1.12e3;
  //This is a float equal to 0.02
  $num = 2e-2;
?>

String

We have already seen numerous examples of string variables on previous pages. A string can contain a single character, or numerous characters.

<?php
  $initial = "A";
  $surname = "Bergeret";
  echo $initial." ".$surname;
?>

Constants

If variables are values which may 'vary' during the life of a script, then a constant is a value which remains 'constant' (does not vary) during the life of that script. Therefore, it is assigned a value which is used throughout the script. Examples would be the number of hours in a day, the number of students on a course, the rate of VAT etc.

<?php
  define ("PI", 3.142);
  // output the value of twice PI
  echo (2 * PI);
?>

This produces:

6.284

NOTE: - Conventions are for constants to be UPPERCASE

Note the syntax of the definition of PI, and the fact that unlike variables, constants do not have a '$' as their first character.

Constants are much underused by new programmers. If used to define the number of lines used on a web page, it can be altered once, without having to make numerous changes throughout the code.

Ready to move on?

PHP105 - Expressions, Operators, and Variable Assignments