PHP variable types. Integers, strings, arrays


Well, now let’s talk about variables. PHP variables are represented by a dollar sign followed by the name of the variable. A variable name starts with a letter or underscore, followed by any number of letters, numbers or underscores. We use variables to set any values that are used in our script. In PHP we assign variable value like this:

$var1 = “hello”;
$var2 = 10; 

There are different types of variables: integers, strings, arrays etc. PHP doesn’t require explicit type definition in variable definition. A variable type is determined by the context in which that variable is used. Our $var1 variable is a string and $var2 is an integer. Note that variable may behave in different manners, depending on what type it is at the time.

Strings are enclosed in single-quotes (‘) or double-quotes (“). Another way to delimit a string is by using here doc syntax (“<<<“). Strings may be concatenated using the ‘.’ (dot) operator.

In PHP you can create both scalar and associative arrays using list() or array() functions or setting each array element value:

$array[0] =”value1″;
$array[1] =”value2″;

PHP is capable of modifying variables passed from HTML forms. Read our form tutorial if you are not familiar with them. When a form is submitted to a PHP script , any variables from that form will be automatically available to the script by PHP. Well, let’s say that we have a simple form:

<form method=”post” action=”ourphpscript.php”>
<p>Enter Your Name: <input type=”text” name=”name” size=”20″><br>
<input type=”submit” value=”Submit”></p>

When this form is submitted the “name” variable will be available as $name for ourphpscript.php PHP script specified in ACTION.

Well, let’s go to the next tutorial PHP operators.