📖 PHP Variables and Data Types

PHP Overview

PHP can be embedded in a regular web page enclosed in <?php /* php code goes here */ ?> tags. It can be intermixed with HTML tags in a variety of ways and may have multiple PHP blocks within the same HTML page. Keep in mind:

  • PHP runs on a PHP-enabled server, and changes are reflected only when redisplayed from the server.
  • Files must be saved with a .php extension for the PHP code to execute.
  • PHP statements do not end with a line break. Use a semicolon (;) at the end of each line to indicate the end of the statement.

Comments in PHP

Comments in PHP code are used for development purposes. They will be ignored by the PHP parser at run time.

Single-line comments
Use // at the beginning of the comment.
Multi-line comments
Use /* and */ to wrap multiple lines.

Variables

Variables in PHP represent values stored in the computer's memory, acting as placeholders for data.

  • Variable names always begin with a $ sign.
  • The first character after the $ cannot be a number.
  • Only letters, numbers, and the _ underscore may be used—no special characters or spaces.
  • PHP variable names are case-sensitive: $Dog is not the same as $dog.
  • Variable names should be meaningful and descriptive, e.g., $name, $age, $payAmount.

Assigning a Value to a Variable

To assign a value to a variable, use the equal sign (=). The value on the right side of the equal sign is assigned to the variable on the left side. Keep in mind:

  • Only a variable name can be on the left side of the equal sign.
  • On the right side, you may have a constant, variable, or calculation.
  • Strings (text characters) must be enclosed in quotes.
  • Numbers should not be enclosed in quotes.

<?php
	$myNumber = 10;
	$myString = "This is my text string";
	// This is a comment
?>

Data Types

PHP supports multiple data types. The most common ones are:

Integer
A number without a decimal point.
Real Number (Floating point or Decimal)
A number with decimal values.
String
A sequence of characters or text.

PHP Output

There are two main ways to output data to the browser in PHP: echo and print. While they are virtually the same, print returns a value of 1, making it slightly slower. echo can accept multiple parameters, although this is rarely needed.

  • To output data, use print or echo followed by a variable, number, or string.
  • Strings can be enclosed in single quotes or double quotes.
    • When a variable is included inside double quotes, its value is displayed.
    • Inside single quotes, the variable name is displayed instead.
  • To concatenate strings, use the dot (.) operator.

PHP Code Output Examples


<?php
	echo 99;
	// Output: 99

	echo "Hello";
	// Output: Hello

	$totalAmount = 100;
	echo $totalAmount;
	// Output: 100

	echo "The total amount is $totalAmount";
	// Output: The total amount is 100

	echo 'The total amount is $totalAmount';
	// Output: The total amount is $totalAmount

	$first = "Patti";
	$last = "Burks";
	print $first . $last;
	// Output: PattiBurks

	print $first . ' ' . $last;
	// Output: Patti Burks

	print "$first $last";
	// Output: Patti Burks

	print 10 + 5;
	// Output: 15

	$x = 2;
	$y = 3;
	print $x * $y;
	// Output: 6
?>