PHP Syntax variables

Source: Internet
Author: User
You can't view the source code in your browser by looking at the origin document-you can only see the output of the PHP file, which is pure HTML. This is because the script has been executed on the server before the result is returned to the browser.

Basic PHP syntax

PHP script block to end. You can place the PHP script block anywhere in the document.

Of course, on a server that supports shorthand, you can use it to start and end script blocks.

However, in order to achieve the best compatibility, we recommend that you use the standard form (

 
  

PHP files usually contain HTML tags, like an HTML file, and some PHP script code.

Below, we provide a simple PHP script that can output the text "Hello World" to the browser:

 
  

Each line of code in PHP must end with a semicolon. A semicolon is a delimiter used to differentiate the instruction set.

There are two basic instructions for outputting text through PHP: Echo and print. In the example above, we used the Echo statement to output the text "Hello World".

Comments in PHP

In PHP, we use//to write single-line comments, or use/* and/* To write large comment blocks.

 
  

Variables are used to store values, such as the results of numbers, strings, or functions, so that we can use them more than once in a script.

Variables in PHP

Variables are used to store values, such as numbers, text strings, or arrays.

Once a variable is set, we can use it repeatedly in the script.

All variables in PHP start with the $ symbol.

The correct way to set variables in PHP is:

$var _name = value;

PHP beginners tend to forget the $ symbol in front of the variable. If you do that, the variable will be invalid.

Let's try to create a variable that holds a string, and a variable that has a value:

 
  

PHP is a loosely-typed language (loosely Typed Language)

In PHP, you do not need to declare a variable before you set it.

In the example above, you see that you do not have to declare the data type of the variable to PHP.

Depending on how the variable is set, PHP automatically converts the variable to the correct data type.

In a strongly typed programming language, you must declare the type and name of the variable before you use it.

In PHP, variables are automatically declared when they are used.

The name of the variable must begin with a letter or underscore "_". Variable names can contain only alphanumeric characters and underscores. Variable names cannot contain spaces. If the variable name consists of multiple words, it should be delimited with an underscore (such as $my _string), or start with a capital letter (such as $myString).

  • 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.