PHP Syntax Basics

Source: Internet
Author: User

PHP variables

PHP variables are used to store characters, numbers, arrays, and even object resources, so that we need to place to use.

$ variable name = value;

The variable name starts with a letter (a-z,a-z) or an underscore _, followed by a

Any letter or number and an underscore, but not a space.

Example:

<?php

$var _char= "Hello";

echo $var _char;

?>

The result: Hello!

extension : With the C language and other strong types of programming language does not pass, PHP

is a loosely typed language that does not need to be preceded by a set of variables

Declares the variable. Depending on how the variable is set, PHP automatically

Variable to the correct data type, and the variable is

is automatically declared when it is used, and this attribute brings the Web programming

A great deal of flexibility.

PHP Constants

The define () function is used to define constants.

Example:

<?php

Define ("CONSTANT", "Hello!");

Echo CONSTANT;

?>

PHP comment symbols and annotation formats

PHP Comment Symbols:

(1) C + + style://This is a c+-style comment.

(2) C Style:/* This is a C-style comment */

(3) Unix shell style: #这是Unix shell-style annotations

Note : do not nest the C-style notation because the system considers the comment to end when it touches the first */, and it is easy to comment out a chunk of code. You can also use//symbols to annotate multiple lines of code multiple times, but typically only limited to a few lines

isset () is used to detect whether one or more variables are set.

Returns TRUE if the detected variable exists, or false after meals, if multiple variables are detected

The test result returns true whenever one of the variables is present.

Example:

<?php

$var = 1;

if (Isset ($var))

{

Echo ' variable $var has been set ';

}

else{

Echo ' variable $var has not been set ';

}

?>

Attention:

(1) isset () can only be used to detect variables, pass any other parameters soy milk causes parsing errors.

(2) Isset () is a language structure and not a function, so it cannot be called by a variable function.

$var =null, the variable is set to NULL.

Unset ($var); The variable that was freed by the unset ().

var $var; Class variables are declared by the Var keyword, but not yet set.

Isset () returns true in the following cases:

$var = "";

$var =array ();

$var = 0;

$var =false;

empty () is used to detect if a variable is empty.

Attention:

(1) empty () can only be used to detect variables, empty (Addslashes ($var)),

This usage is illegal.

(2) empty () is a language structure and not a function, so it cannot be called by a variable function.

Unset () is used to destroy one or more variables.

Note: strictly speaking, destroying a static variable with unset () only breaks the reference between the variable name and the value of the variable.

unset () global variables as with unset () static variables, if you unset () a global variable in a function, only the local variable is destroyed, and the variable in the calling environment keeps the same value as before the call to Unset ().

Tips

    • In many cases, you can have PHP automatically manage variables without having to consider unset () variables. Only when a certain variable needs to be empty is it used. such as: When the user exits the login, in order to ensure the security of information, you can use unset () to clear the contents of the session.
    • Unset () Try not to use static variables, as this is contrary to the original intention of defining static variables.
    • Unset () is a language structure and not a function, so it cannot be called by a variable function.

PHP Data type

Four scalar types: string (String), Integer (integer), float (floating-point, Double), Boolean (Boolean).

Two kinds of composite types:

1.array (Array) 2.object (object).

Two special types:

1.resource (Resource) 2.NULL (empty)

tip: 1. For historical reasons, if the data is float type, the GetType () function returns a double instead of float; 2. If you want to see the value and type of an expression, use the Var_dump () function.

integral type (slightly) , string type (slightly), floating Point Type : The word length of a floating-point number is platform-dependent, although the usual maximum is

1.8e308 and has a precision of 14-bit decimal digits (64-bit IEEE format).

Array: Create a new array using the array () language structure;

$arr =array ("foo" = "Bar", 12=>true);

echo $arr < "foo" >; Output bar

Echo $arr <12>; Output 1

In the example, "foo" and 12 are called key names (key), and "bar" corresponds to the value of true.

in this Array , contains 2 types.

1. $arr ["foo"]: The value is "bar", the string type.

2. $arr [12]: The value is true, Boolean.

object: object to instantiate an object with the New keyword.

Resource: A resource is a special type of variable that holds a reference to an external resource,

such as open file, database connection, graphics canvas area, etc. Resources are created and used through specialized functions.

Note: since the PHP4 Zend engine introduces a resource counting system, it can automatically detect that a resource is no longer referenced (as in Java). In this case, all external resources used by this resource will be released by the garbage collection system. Therefore, it is seldom necessary to manually free up memory with some free-result functions. Note: Persistent database connections are special, and they are not destroyed by the garbage collection system.
Null: Indicates that a variable has no value, and the only possible value of the null type is NULL.

When a variable is assigned a value of NULL, or has not yet been assigned, the unset () can be considered null.

Hint: null cannot be written null.

PHP Functions PHP built-in functions commonly include "String functions", "Array Functions", "Database Functions", "Time/Date Functions", "File Functions", "Image Functions", "regular Functions", "URL functions" and so on.

The Function_exists () function is used to detect whether a function is defined.

Example 2, the detection system built-in function, the following code is common to check whether the system opens the GD library :

<?php if (function_exists (' Gd_info '))
{     echo ' GD library is already open. ‘; }
else {   echo ' GD library is not open. ‘; }?>

Special cases

The Function_exists () function has a special case where function_exists () returns the original function value directly when the parameter is passed in as a function_name () instead of a string function name.

<?php function TestFunc () {     

Run the example output:

I am a custom 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.