How to add a variable to a PHP string

Source: Internet
Author: User
Tags php language learn php

This article is mainly to share with you how to add variables to the PHP string, when defining a string, either single quotation marks or double quotation marks are possible. My personal habit is to use double quotes. In the output string, if the string contains string variables, the use of single and double quotation marks is different. such as the following procedures:

<?php$website = "Nowamagic"; $name = ' gonn '; Echo ' Welcome to visit $website. My name is $name. '; Echo ' <br> ', Echo ' Welcome to visit $website. My name is $name. ";

Program output:
Welcome to visitWebsITe.M y NameIswebsite. MynameisName.
Welcome to visit nowamagic. My name is gonn.
With single quotes around the output, and not parsing the string variable, and using double quotation marks around the output, the variable is parsed, the value of the output variable.
Efficiency issues
It is generally accepted that in a string output that does not require variable parsing, a single quote speed may be faster.
By the way, for a pure string that does not require a variable substitution, it is known that double quotation marks are good in this case, because they represent strings in C/s + +. In addition, for the standard, the attribute values in HTML should be enclosed in double quotation marks, so don't get used to single quotes and abuse everywhere.
Teach you how to add a variable to a PHP string

When we learn PHP language, we will master the PHP string skillfully, then in the actual application, how can we add variables to the PHP string? Today we are going to give you a concrete solution.

<?php       $temp = "Hello"       echo "$temp World";

However, it is necessary to add a variable description to the PHP string, although there is no error in the following example:

<?php       $temp = Array ("One" = 1, "one" = 2);       Output:: The first element is a 1       echo "The first element is $temp [one].";

However, if there is no double quotation mark in the back of the Echo statement, it is necessary to give an error, so we recommend using curly braces:

<?php       $temp = Array ("One" = 1, "one" = 2);       echo "The first element is {$temp [" one "]}.";

These are the specific workarounds for adding variables to the PHP string.

1

When defining a string, it is possible to use either single or double quotation marks. My personal habit is to use double quotes.
In the output string, if the string contains string variables, the use of single and double quotation marks is different. such as the following procedures:

<?php$website = "Nowamagic"; $name = ' gonn '; Echo ' Welcome to visit $website. My name is $name. '; Echo ' <br> ', Echo ' Welcome to visit $website. My name is $name. ";

Program output:
Welcome to visitWebsITe.M y NameIswebsite. MynameisName.
Welcome to visit nowamagic. My name is gonn.
With single quotes around the output, and not parsing the string variable, and using double quotation marks around the output, the variable is parsed, the value of the output variable.
Efficiency issues
It is generally accepted that in a string output that does not require variable parsing, a single quote speed may be faster.
By the way, for a pure string that does not require a variable substitution, it is known that double quotation marks are good in this case, because they represent strings in C/s + +. In addition, for the standard, the attribute values in HTML should be enclosed in double quotation marks, so don't get used to single quotes and abuse everywhere.
Teach you how to add a variable to a PHP string

When we learn PHP language, we will master the PHP string skillfully, then in the actual application, how can we add variables to the PHP string? Today we are going to give you a concrete solution.

<?php       $temp = "Hello"       echo "$temp World";

However, it is necessary to add a variable description to the PHP string, although there is no error in the following example:

<?php       $temp = Array ("One" = 1, "one" = 2);       Output:: The first element is a 1       echo "The first element is $temp [one].";

However, if there is no double quotation mark in the back of the Echo statement, it is necessary to give an error, so we recommend using curly braces:

<?php       $temp = Array ("One" = 1, "one" = 2);       echo "The first element is {$temp [" one "]}.";

These are the specific workarounds for adding variables to the PHP string.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.