Initial knowledge of PHP

Source: Internet
Author: User
Tags echo command variable scope

Initial knowledge of PHP

Although is to do the front-end, but usually read, do projects will be related to the back-end PHP, but not very understanding, and often listen to the PHP big God said: PHP is the best language in the world! So, learn and summarize PHP through this blog post to get to know the "best language in the world".

  

The first part: Introduction to PHP

The full name of 1.PHP is hypertext preprocessor, the hypertext preprocessor, which is a common open source scripting language . We know that JavaScript is also a scripting language that runs on the browser to control page content, while PHP is running on the server-side scripting language to read the database and display the content on the page. In addition,PHP is open-source, free to use , so there are countless folk experts, contribute to the use of many of the class library, so that it has a strong vitality, There are some advantages over Microsoft's ASP. NET, and PHP can run on a variety of platforms, but ASP. NET can only run under the Windows platform. Of course, there are many good places in ASP. PHP is not available.

2. What is a php file then? We can sometimes see a file with a. php suffix, which is actually a PHP file that can contain text, HTML, JavaScript code, and PHP code, and the PHP code executes on the server, and the results are returned to the browser in plain HTML.

3.What can PHP do?

    • Can generate dynamic page content (this is implemented via AJAX at the front end)
    • You can create, open, read, write, and close files on the server
    • You can collect form data ( the form that we write on the front end includes name value, which is related to PHP)
    • You can add, delete, and modify data in our database

  Of course, in addition, PHP has a lot of powerful features. Through PHP, we are no longer limited to output HTML, but also output images, PDF files, flash movies and so on.

4. Why use PHP? As described earlier, it can be run on different platforms (Windows, Linux, UNIX, MAC OS X, etc.), and it is compatible with almost all of the servers currently in use (Apache, IIS, etc.).

  

Part II: PHP Syntax A. Basic PHP syntax is as follows:

  PHP scripts can be placed anywhere in the document, the PHP script begins with <?php, ends with?>, with the following code:

<?php    //php Code?>

The default file name extension for php files is. php, which usually contains HTML tags and some PHP script code, such as the following PHP file instance:

<! DOCTYPE html>

The results of the operation are as follows:

For the above PHP file, the following points need to be noted:

    • I did not add the <meta> tag in the file, actually can add, but I omitted it
    • We notice that each line of code in PHP must end with a semicolon , a delimiter that distinguishes the instruction set.
    • The echo command is used for the output, plus the Print command. Note pronunciation:

  

In addition, I used comments in the PHP code. is a single-line comment, and JavaScript is the same. Multi-line annotations can be used with/* */.

  

  

Two. Variables in PHP 1. Basic Concepts

As with all other languages, variables are "containers" that store information. similar to algebra, you can assign a value (such as x=10) or an expression (z=x+y) to a PHP variable. There is no doubt that a variable can be a short name (such as x and y with only one letter) or a more descriptive name (such as age, Carname, Totalvolume). The rules are as follows;

    • The variable must begin with the $ symbol followed by the name of the variable.
    • The variable name must begin with a letter or an underscore character (note: In JavaScript, the variable must start with a letter, underscore, or $, where the knowledge must be a variable with a $ description)
    • Variable names can contain only letters, numbers, and underscores (A-Z, 0-9, and _), which is the same as JavaScript.
    • The variable name cannot contain spaces.
    • Variable names are case-sensitive. (Note:PHP statements and PHP variables are case-sensitive )

  

2. Create (declare PHP variable)

In fact, PHP does not have a variable declaration command. The variable is created when we assign it to it for the first time. is as follows:

<?php$txt= "Hello World"; $x =5; $y =10; $z = $x + $y; >

With the PHP code above, we actually created the $txt variable, $x variable, $y variable, $z variable.

We can see from the above example that:

    • When assigning a literal value (that is, a string) to a variable, double quotation marks must be added to both sides of the text value.
    • As you can see, we do not specify the variable's data type when declaring the variable, so php and JavaScript are weakly typed languages. Strongly typed languages such as Java, C + + are the types of variables (such as int double float string, etc.) and names that need to be declared (defined) before using variables. as a result, PHP automatically converts variables into the correct data type based on the value of the variable.

    

3. Variable Scope

As with all other languages, the scope of a variable is the part of the script in which variables can be referenced/used.

    There are four different variable scopes in PHP. Are local, global, static, parameter, respectively.

  (1) First, global variables and local variables are discussed.

Global variables and global scopes : variables defined outside of all functions, with global scope. In addition to functions, Global variables can be accessed by any part of the script, and to access global variables in a function, you need to use the Global keyword. ( key: The global variables here cannot be accessed by the Function!!!!) )

     Local Variables: variables declared inside a PHP function are local variables that can only be accessed inside the function (this is the same as JavaScript).

Examples are as follows:

  

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

The results are as follows:

  

Several discussions:

    • When you call the MyTest () function, only y is output, because Y is a local variable within the function and no output x! (As mentioned earlier, the function cannot directly access global variables )
    • When the external outputs x and y directly, only x is output, because X is a global variable, and there is no output y because the local variable is Y.
    • We can see the code echo "variable x is: $x"; In PHP you can put variables in the string output (and in JavaScript you have to use + to splice variables and strings).
           

  

  

Initial knowledge of PHP

Related Article

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.