The basics of PHP syntax summary and variable _php techniques

Source: Internet
Author: User
Tags html tags php basics

Recently has a H5 project needs, needs the service side, after the study decided to use the PHP to implement an HTTP service end, then began to revisit the PHP syntax, by the way records the main point, in order to revisit at any time. Content excerpt from W3school PHP Manual, and in accordance with their own understanding of the reorganization.

What is PHP?

PHP is the acronym for "PHP Hypertext Preprocessor"
PHP code executes on the server, and the result returns to the browser in plain text
PHP files can contain text, HTML, CSS, and PHP code
The suffix of the php file is ". php"
PHP scripts can be placed anywhere in the document.
PHP files usually contain HTML tags and some php scripting code.

PHP Basics Syntax

Script to end
The statement ends with a semicolon (;).
The closing tab of the code block also automatically indicates a semicolon (so you do not have to use semicolons on the last line of the PHP code block).
Or # represents a single-line comment
/**/is multiline comment
Variable Case sensitive
User-defined functions, classes, and keyword capitalization are not sensitive (for example, if, else, Echo, and so on)

PHP Constants

Constants cannot change or undo definitions once they are defined
Constants throughout the entire script are automatic global
Set constants use the Define () function, which uses three parameters:

First parameter definition constant name
The second parameter defines a constant value
(optional) The third parameter stipulates whether the constant name is case-sensitive and false by default.

<?php
  define ("greeting", "welcome!");
  echo greeting;  Case sensitive constant

  define ("Hello", "welcome!", true);
  echo Hello;   Case insensitive constant
?>

Valid constant names are opened with characters or underscores

PHP variables

Variable weak type
The variable begins with the $ symbol followed by the name of the variable, such as $x = 5;
A variable declared outside of a function owns the Global scope and can only be accessed outside of the function.
Variables declared within a function have a local scope and can only be accessed within a function.
The global keyword is used to access globally variables within a function. To do this, use the Global keyword before the variable (inside the function):
Example 1:

<?php
 $x =5;           Global scope function
 myTest () {
  $y =10;         The local scope
  echo "variable x is: $x";  Do not output
  echo "Variable y is: $x";  Output
 } 

 myTest ();

 echo "Variable x is: $x";   Output
 echo "Variable y is: $x";   Do not output
?>

Example 2:

<?php
 $x =5;
 $y =10;

 function MyTest () {
  global $x, $y;
  $y = $x + $y;
 }

 MyTest ();
 echo $y;         Output
?>

PHP Static Keywords

Typically, all variables are deleted when the function completes/executes. However, sometimes I need not delete a local variable. Achieving this requires further work.
To do this, use the static keyword when you first declare a variable:

<?php
 function MyTest () {
  static $x =0;
  echo $x;
  $x + +;
 }

 MyTest ();  Output 0
 myTest ();  Output 1
 myTest ();  Output 2
?>

Then, whenever the function is invoked, the information that the variable stores is the information it contains when the function was last called.
Note: The variable is still a local variable of the function.

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.