Introduction to PHP for Os X by Example

Text, Numbers & Variables

  • Basic variables in PHP can contain strings or numbers.
  • Variable names should start with $ and contain only alphanumeric characters and underscores. They should not start with a number.
  • PHP makes a distinction between integers and floating-point numbers when storing them internally. Floating point numbers may be stored inaccurately.
  • Use a single period to concatenate strings.
  • The print() language construct is used to output data to the browser window.
  • Single quoted strings can not interpolate variables (see below) and to use single quotes or back slashes in them they must be escaped using a back slash.
  • Using double-quotes instead of single-quotes we can take advantage of interpolation.
  • The concatenation operator in conjunction with the assignment operator (.=) appends a string to a string.
  • The arithmetic operators perform arithmetic operations (+, -, *, /, %, ++, –).
  • Arithmetic operators in conjunction with the assignment operator (+=) perform the arithmetic operation on the left hand variable with the right-hand variable. e.g. $n = 2; $n += 2; $n is now 4.
  • We can convert strings to numbers and vice-versa by using the appropriate operators (period for strings, plus sign for numbers).
  • The printf() function can be used to control the formatting of print output.
  • Here document syntax can be used to print out large blocks of data and does not require double quotes to be escaped.

Resources