PHP echo

echo() prints out a string on the screen. when double quotes are used, variables can be included, while single quotes can not.

<?PHP
	echo "PHP Tutorial"; //PHP Tutorial
	echo 'PHP Tutorial'; //PHP Tutorial
	$a="Joni";
	echo "$a PHP Tutorial"; //Joni PHP Tutorial
	echo '$a PHP Tutorial'; //$a PHP Tutorial
?>

Using <<< to print out multiple lines.

<?PHP
//MM is self defined, as long as MM is also used to end the print out
echo <<<MM  
  This is Line 1
  Line 2
MM;
?>

Special characters include double quote, single quote should be escaped using \.

<?PHP
	echo "John's daughter";  //John's daughter
	echo 'John's daughter';  //Error
	echo 'John\'s daughter';  //John's daughter
	echo 'John"s daughter';  //John"s daughter
	echo "John"s daughter";  //Error
	echo "John\"s daughter";  //John"s daughter
?>

Strings can be concatenated by symbol.

<?PHP
	$arr = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
	foreach($arr as $dy)
	{
	   echo "Today is: " . $dy . ".";
	}
	Today is: Monday.
	Today is: Tuesday.
	Today is: Wednesday.
	Today is: Thursday.
	Today is: Friday.
	Today is: Saturday.
	Today is: Sunday.   
?>

Echo array element.

<?PHP
	$arr = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
 	for ($i=0;$i<3;$i++)
	{
		echo "$arr[$i]";  //OK
		echo "$arr[$i + 1]"; //Error
		$k=$i+1;
		echo "$arr[$k]";  //OK
	}
?>

Echo the array element of a class object.

<?PHP
	class c
	{
		var $arr;
		public __construct()
		{
		   $this->arr = array();
		}
	}

	$b = new c();
	$b->arr = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
	for ($i=0;$i<3;$i++)
	{
		echo "$b->arr[$i]";  //Error
	}
	$arr2 = $b->arr;
	for ($i=0;$i<3;$i++)
	{
		echo "$arr2[$i]";  //OK
	}	
?>



:: PHP Tutorials Home ::
PHP String Functions
 • concatenation • echo
 • ereg • ereg_replace
 • explode • htmlspecialchars
 • preg_match • preg_replace
 • preg_split • print,sprintf
 • regular expr. • str_replace
 • strcmp • strpos
 • strrev • strrpos
 • strtr • substr
 • substr_replace
PHP Array Functions
 • array_diff • array_flip
 • array_intersect • array_key_exists
 • array_keys • array_merge
 • array_pop • array_push
 • array_rand • array_search
 • array_splice • array_unshift
 • array_values • asort & arsort
 • count • in_array
 • ksort • shuffle
 • sort
PHP Data Types
 • array • associative array
 • date & time • number
 • class, object • regular expression
 • string • variables
PHP Loop & Conditions
 • continue & break • for loop
 • foreach • if else
 • not equal • while
PHP File System Functions
 • copy • delete, unlink
 • dirname • download url
 • file_exists • is_file
 • mkdir • read file
 • scandir • write file
PHP Popular Topics
 • ajax • clone
 • comments • constants
 • cookie • database
 • defined • die
 • form validation • gd, draw images
 • global variables • header url
 • heredoc • mail
 • pass by reference • print
 • regular expr. • sessions
 • threads • xml parse
PHP Math Functions
 • abs • cos
 • exp • floor & ceil
 • fmod • log
 • max & min • pow
 • round • sin
 • sqrt • tan
endmemo.com © 2024  | Terms of Use | Privacy | Home