PHP

php echo command with examples

Description:

PHP echo command with examples-hello everyone in this article what I’ll be covering is the PHP echo command, and I know I have been using the PHP echo command several times in the previous articles. but the echo command is probably gonna be one of the most used PHP commands that you use while you’re coding PHP code so it really does deserve its own article because you can do so many great things with the PHP echo command so let’s just go ahead and jump into coding…


Php echo command:

With the PHP echo command you can output content or text to the browser you know by sending that data to the server and the server sends that back to the browser is plain HTML so let’s just review a little bit about how you can output a string or integer to the browser. For example

I use a variable at this time let’s say we have a variable called  $aString and we’re gonna assign the string hello programming digest to that variable

and let’s create another variable and we’re gonna call it int and we’re gonna assign the value 300 to it

In the above instruction, we can tell PHP that aString again is a string variable because of the double quotes and we can tell that int is an integer variable because it doesn’t have quotes.



so what the PHP echo command, what we can do here is an echo out what’s inside aString variable simply by typing echo aString and followed by a semicolon

go ahead and save the above code into your folder which is in your htdocs folder

php echo

 

I’m gonna save it with the name echo.php.  now type localhost slash foldername slash filename into your browser and press enter

php echo

so as we see in the below figure it says programming digest

php echo


Html tags in PHP echo:

we can also use HTML tags within the PHP echo command so let’s say for example I’m gonna use the same variables here let’s say I’m gonna make programming digest text bold, and again I expect you to know some of the basic concepts of some of the basic HTML tags that you can use

as you can see I used the bold tag so the text should be bold. so I’m just gonna go ahead and save the file and refresh the browser and as you can see programming digest is bold

php echo


let’s make programming digest text underlined, I am using the same variables as you can see

so again these are just some basic HTML tags, so save the file and refresh the browser and then you see programming digest.. is underlined

php echo

so it’s really neat that you can use HTML within PHP but it’s very important that you use the correct HTML syntax or of course, you’re gonna have issues or so now that I have exposed you to the concept of using HTML with a PHP echo command.


Php echo Single quotes and double quotes:

you really have to be careful when echoing quotes or double quotes the reason being is any other string or HTML code that uses quotes can also cause problems when using the PHP echo command see the echo uses quotes to define the beginning of the string and the end of the string.

php echo

for example, we want to display an image in the web browser so I’m gonna say

normally we would put double quotes here but remember this echo is looking for the start of the string which is indicated here and then the end which is indicated by another double quote so we can’t use the double quote there so we have to use a single quote and followed by that we can put the URL of the image

then end it with another single quote then followed by a greater than sign then followed by a double quote and then a semicolon.


Save the above code and refresh the browser

php echo

As you can see our image is displayed in a web browser. So in this way we use single and double quotes in PHP echo.



Difference between PHP echo and PHP print commands:

We have already seen various ways of using the PHP echo command to output text from the server to the browser. In some cases, a string literal is displayed, in others, strings were concatenated or the values ​​of variables were calculated first. The output was also shown spanning multiple lines.

But there is an alternative to the PHP echo command that you can also use: the print command. These two commands are very similar to each other, but print construction, like a function that takes a single parameter and has a return value (which is always 1), and echo is a pure PHP construct.



In general, PHP echo works faster than print on plain text output because it does not set the return value. On the other hand, since it is not a function, unlike print, it cannot be used as part of a more complex expression. The following example uses the print function to display information about whether the value of a variable is TRUE or FALSE, but it is not possible to do the same with the PHP echo command since it will display a message Parse error:

php echo

Using a ternary operator question mark ? is the easiest way to ask if the value of $b is true or false. The command to the left of the colon is executed if $b is true value, and the command to the right is executed if $b has false. However, the most common use of the examples here is an echo, and I recommend that you use it until you really need to use print in PHP development.

 

Related Articles

One Comment

  1. This article is a rare gem in the world of online media. The author’s writing is both substantive and engaging, and their commitment to presenting factual and balanced perspectives is truly commendable. What I appreciate most about this article is its ability to take complex issues and present them in a way that is accessible and engaging for readers of all levels, making it a valuable resource for anyone looking to stay informed on important social and political issues.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button