PHP Programing language

adplus-dvertising
PHP Echo
Previous Home Next

The PHP command echo is a meaning for the outputting text to the web browser. and it's throughout PHP career you will be using the echo command more than any other. So let's give it a solid perusal.

Outputting a string

To output a string, then using PHP echo. You can place either a string variable or you can use quotes, like we do below, to create a string that the echo function will output.

PHP Code:

<?php
$myString = "r4rtechsoft";
echo $myString;
echo "<h1>This sites is very useful for programming </h1>";
?>

Output:

r4rtechsoft

This sites is very useful for programming.

In this example we output "r4rtechsoft" without a hitch. The text we are outputting is being sent to the user in the form of a web page, so it is important that we use proper HTML syntax!

In our second echo statement we use echo to write a valid Header 5 HTML statement. To do this we simply put the <h1> at the beginning of the string and closed it at the end of the string. Just because you're using PHP to make web pages does not mean you can forget about HTML syntax.

What is careful echoing quotes

It is pretty cool that you can output HTML with PHP. However, you must be careful when using HTML code or any other string that includes quotes! Echo uses quotes to define the beginning and end of the string, so you must use one of the following tactics if your string contains quotations:

  1. Don't use quotes inside your string
  2. Escape your quotes that are within the string with a backslash. To escape a quote just place a backslash directly before the quotation mark, i.e. \"
  3. Use single quotes (apostrophes) for quotes inside your string.

Echoing variables

Echoing variables is very easy. The PHP developers put in some extra work to make the common task of echoing all variables nearly foolproof! No quotations are required, even if the variable does not hold a string. Below is the correct format for echoing a variable.

PHP Code:

<?php
$my_string = "radheshyam is very good boy: ";
$my_number = 4;
$my_letter = a;
echo $my_string;
echo $my_number;
echo $my_letter;
?>

Output:

radheshyam is very good boy: 4a

Echoing Variables and Text Strings

we write the variables inside of double-quoted strings (e.g. "string here and a $variable"). Then using the write variables putting a variable inside the quotes (" ") you are telling PHP that you want it to grab the string value of that variable and use it in the string. The example below shows an example of this cool feature.

PHP Code:

<?php
$my_string = "radheshyam is very good boy: ";
echo "$my_string Bobettta <br />";
echo "hello friend my name is radheyshyam: $my_string <br />";
echo "hello friend my name is radheyshyam: $my_string Bobetta";
?>

Php echo - not a function

Echo is not a function, rather it is a language construct. When you use functions in PHP, they have a very particular form, which we will be going over later. For now, just know that echo is a special tool that you'll come to know and love!

Previous Home Next