PROWAREtech

articles » current » php » tutorial » page-1

PHP: Tutorial - Page 1

Variables, strings, objects and arrays.

Entering and Exiting PHP Mode

PHP code is embedded in the HTML so all pages are capable of processing and containing HTML. PHP considers all text it finds in the file to be HTML until it finds one of four things:

  • <?php
  • <?
  • <%
  • <script language="php">

To exit PHP mode, use ?>, %> or </script>. This tutorial will use <?php.

Here is an example PHP file:

<html>
	<head>
		<title>
		<?php
			echo 'HELLO PHP!';
		?>
		</title>
	</head>
	<body>
		<?php
			// THIS IS A COMMENT
			for($i = 0; $i < 10; $i++) {
				echo $i.'<br />';
			}
		?>
	</body>
</html>

echo is a language construct that writes to standard output. The print function can be used identically.

Variables

All PHP variables start with a dollar sign ($). They are interchangeable which means PHP is a weakly typed language because it does not have different types of variables. One variable handles numbers, strings, booleans, arrays, etc. In the above example, $i is a variable and the keyword echo outputs data to the screen.

integer, float, string and boolean are simple types. array, object and resource are complex types.

Strings

Strings are simply an array of bytes (ASCII, not UTF-8). Example strings are 'this is a test', "this is a test", and even numbers when inside of quotes "0123456789".

Single Quote Strings

Single quote strings like 'this is a single quote string' have only two special characters that need to be escaped: the single quote and the backslash. As a result, single-quoted strings are more efficient. For example, 'I\'ll go to the C:\\ drive or lose $20.' would print this:

I'll go to the C:\ drive or lose $20.

Double Quote Strings

This table shows the double-quoted string escape sequences:

escape
sequence
character
\n newline
\r carriage return
\t tab
\\ backslash
\$ dollar sign
\" double quote
\0 to \777 octal value
\x0 to \xFF hex value

For example, the string "The so called \"car\"\ncost \$25,999!" would print this:

The so called "car"
cost $25,999!

Variable Substitution

Double quote strings feature variable substitution. Single quote strings do not have this making them faster. Run this script to compare them.

<?php
	$num = 14;

	echo "I have $num plants.";
	echo 'I have $num plants.';
?>

Objects

Objects are used to define and manipulate a set of variables that belong to a unique entity. Each object has it own set of variables and functions that operate on those variables.

Resources

PHP has many extensions available to it that allow it to manipulate graphics, connect to databases, or make calls to Java programs. Because these are external systems then they need to have types of data unique to them that PHP cannot represent using any of the other data types. These data types are meaningless to PHP but can be used by the external libraries that created them.

Arrays

An array is made up of many elements. Each element has a key that defines its place in the array. An array can have only one element with a given key. Each element also has a value, which is the data associated with the key. Finally, each array has a cursor, which points to the current key.

Keys can be almost anything: integers, objects, strings, or other arrays. Floating-point numbers may not be used as keys.

Use the pseudo-function array to create a new array and the [] operator to add items to an array.

<?php
	$a = array();

	$a[2] = "HELLO"; // key equals 2
	$a[] = "PHP!"; // no key
	$a["jackpot"] = "win"; // key equals "jackpot"
	$a["test"] = array(0 => "YES!"); // key equals "test", value is an array; 0 is a key, "YES!" is the value
	$a["test"][1] = "NO!"; // multi-dimensional array

	echo $a[2];
	echo $a["jackpot"];
	echo $a["test"][0];
	echo $a["test"][1];
?>

Determining the Data Type

Use the gettype function to return the type of a variable. Valid return values are 'integer', 'string', 'double', and 'object'. gettype takes one parameter: a variable to evaluate.

<<<[Page 1 of 4]>>>

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
CLOSE