Home >Backend Development >PHP Tutorial >PHP basic tutorial echo command

PHP basic tutorial echo command

WBOY
WBOYOriginal
2016-07-25 08:57:131503browse
This article introduces the most basic command in PHP programming, the specific use case of the echo command, for your reference.

The PHP command echo is a means of outputting text to a web browser.

Throughout your PHP career, you will use the echo command more often than any other command.

So, let’s give it a solid and read it carefully!

Output a string

To output a string, like we did in the previous lesson, use PHP's echo.

You can put it into one of the string variables, or you can use quotes, like we do below, to create a string that the echo function will output.

Code:

<?php
$myString = "Hello!";
echo $myString;
echo "<h5>I 喜欢 PHP!</h5>";
?>

Result: Hello! I love PHP!

In the above example, we output "Hello!" successfully. The text we output is sent to the user in the form of a web page, so it's important that we use proper html syntax!

We use echo in our second echo statement to write a valid header 5 HTML statement. To do this, we simply put

at the beginning of the string and close it at the end of the string. Just because you use PHP to make web pages doesn't mean you can forget about HTML syntax!

Be careful with the echoing quotes! Can output HTML and PHP. However, you have to be careful when using html code or any other string, including quotes! echo uses quotes to define the beginning and end of a string, so you must use one of the following strategies if your string contains quotes:

Don’t use quotes inside your strings

Escape your quotes by using a backslash in the string. Just to escape the quotes put a backslash directly before the quotes i.e. " Use single quotes (apostrophes) for strings inside quotes. See the example below where correct and incorrect usage of back is shown:

Code:

<?php
// This won't work because of the quotes around specialH5!
echo "<h5 class="specialH5">I love using PHP!</h5>";  

// OK because we escaped the quotes!
echo "<h5 class=\"specialH5\">I love using PHP!</h5>";  

// OK because we used an apostrophe '
echo "<h5 class='specialH5'>I love using PHP!</h5>";  
?>

If you want to output a string that includes quotes, use a single quote (') or an escaped quote and place a backslash (") in front of it. The backslash tells PHP that you want quotes to be used within the string and not The string used to terminate echo

.

Echo variables Endorsing variables is easy. The developers of the PHP language put in some extra work to make all the variables that echo the common task almost foolproof! No quotes are required, even if the variable does not hold a string. Below is the correct format for the echo variable.

Code:

<?php
$my_string = "Hello Bob.  My name is: ";
$my_number = 4;
$my_letter = a;
echo $my_string;
echo $my_number;
echo $my_letter;
?>

Result: Hello Bob. My name is: 4a

Echo variables and text strings

You can also put double quotes inside a string variable (e.g. "string and variable"). By putting quotes ("") inside a variable, you are telling PHP that you want it to grab the string value of that variable and use it in the string. Consider the following example. Code:

<?php
$my_string = "Hello Bob.  My name is: ";
echo "$my_string Bobettta <br />";
echo "Hi, I'm Bob.  Who are you? $my_string <br />";
echo "Hi, I'm Bob.  Who are you? $my_string Bobetta";
?>

Result: Hello Bob. My name is: Bobetta Hi, I'm Bob. Who are you? Hello Bob. My name is: Hi, I'm Bob. Who are you? Hello Bob. My name is: Bobetta

By putting a string variable inside, you save yourself some time and make your code easier to read, but it does take some getting used to. Remember to use double quotes, single quoted string values ​​will not be grabbed. Single quotes will just output the variable name as a string, like )$my_string) instead of (Hi my name is Bob. :).

PHP’s 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 special form, which we will look at later. For now, just know that echo is a special tool that you'll come to know and love!

This command about the echo echo content in php is introduced here. I hope it will be helpful to everyone.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn