Free Web Tutorials Free Online Tutorials About Us Contact Us

Php Tutorial Variables:


PHP Variables may contain strings, numbers, or arrays. Below, the PHP script assigns the string "Hello World" to a variable called $txt:

<?php
	$txt="hello world";
	echo $txt;
?>


The output when uploaded to your web server would be hello world.

To concatenate two or more (String) variables together, use the dot (.) operator:

<?php
	$txt1="hello world";
	$txt2="1234";
	echo $txt1 . " " . $txt2 ;
?>


The output when uploaded to your web server would be hello world 1234.

PHP also contains the standard arimetic operators + - / * %. As we mentioned earlies all PHP variable may contain strings, numbers, or arrays. For numbers we drop the inverted commas ""; example

<?php
	$add = 4 + 4; 
	$subtract = 8 - 4; 
	$multiplication = 5 * 3; 
	$division = 12 / 3; 
	$modulus = 5 % 2; 

	echo "add 4 + 4 equals $add";
	echo "subtract 8 - 4 equals $subtract";	
?>


As you can see above $add has now been given the value 8. Echo should output to the screen add 4 + 4 equals 8.


free web tutorials php variables tutorials