PHP Basic Syntax

Source: Internet
Author: User
Tags learn php alphanumeric characters

Basic PHP syntax

The PHP script can be placed anywhere in the document.

The PHP script begins with <?php and ends with ?> :

<?php//here is the PHP code?>

The default file name extension for PHP files is ". php".

PHP files usually contain HTML tags and some php script code.

PHP Case Sensitive

In PHP, all user-defined functions, classes, and keywords (such as if, else, Echo, and so on) are not case sensitive.

In the example below, all of these three-day echo statements are legal (equivalent):

Instance

<! DOCTYPE html>

In PHP, however, all variables are case-sensitive.

In the following example, only the first statement shows the value of the $color variable (because $color, $COLOR, and $coLOR are treated as three different variables):

Instance

<! DOCTYPE html>

PHP variables

A variable is a container for storing information:

Instance

<?php$x=5; $y =6; $z = $x + $y; echo $z;? >

Running an instance

Similar algebra

X=5y=6z=x+y

In algebra we use letters (such as x) to hold values (such as 5).

From the above expression Z=x+y, we can calculate the value of Z is 11.

In PHP, these three letters are called variables .

Note: Consider variables as containers for storing data.

PHP Variable rules:

    • The variable starts with the $ sign, followed by the name of the variable

    • Variable names must begin with a letter or underscore

    • Variable names cannot start with a number

    • Variable names can contain only alphanumeric characters and underscores (A-Z, 0-9, and _)

    • Variable names are case sensitive ($y and $Y are two different variables)

PHP is a loosely-typed language

In the example above, please note that we do not have to tell the data type of the PHP variable.

PHP automatically converts the variable to the correct data type based on its value.

In languages such as C and C + + and Java, a programmer must declare its name and type before using a variable.

PHP variable Scope

In PHP, variables can be declared anywhere in the script.

The scope of a variable refers to the part of the script that a variable can be referenced/used.

PHP has three different scope of variables:

    • Local (partial)

    • Global (globally)

    • Static (statically)

Local and Global Scopes

Variables declared outside the function have Global scope and can only be accessed outside of the function.

Variables declared inside a function have a local scope and can only be accessed inside the function.

The following example tests for variables with local and global scopes:

Instance

<?php$x=5; Global scope function MyTest () {  $y = 10;//local scope  echo "<p> variable:</p> inside the test function";  echo "Variable x is: $x";  echo "<br>";  echo "Variable y is: $x";} MyTest (); echo "<p> variable:</p> outside the test function"; echo "variable x is: $x"; echo "<br>" echo "Variable y is: $x";? >

PHP Global Keywords

The global keyword is used to access variables within the function.

To do this, use the Global keyword before (inside the function) variable:

Instance

<?php$x=5; $y =10;function myTest () {  global $x, $y;  $y = $x + $y;} MyTest (); Echo $y; Output 15?>

PHP Echo and Print statements

The difference between Echo and print:

    • echo-capable of outputting more than one string

    • Print-only one string is output and always returns 1

echo "I ' m about to learn php!<br>"; echo "This", "string", "is", "made", "with multiple parameters." ;

This article explains the basic PHP syntax, more about the content, please follow the PHP Chinese web.

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.