PHP Novice Learning Variables and Constants _php Tutorial

Source: Internet
Author: User
Tags define function php form variable scope vars
In the course of a change, the amount that is always constant, we call it Constants, while the variable is variablesis a contiguous storage space with a name. The following is a description of PHPBasic knowledge of variables and constants.

To get started with PHP variables, this introductory tutorial is described in the following sections: How PHP variables are identified, how PHP variables are declared, how to assign values to PHP variables, the types of PHP variables, and the commonly used PHP variable functions.

For PHP constants to learn, the main introduction to the definition of PHP constants and how to use.

How PHP variables are identified

The so-called identifier, in fact, is the PHP variable name, mainly in letters, numbers, underscores and dollar sign ($), the length can be arbitrarily long, cannot start with a number, remember in PHP, the variable is case-sensitive (PHP system comes with the exception of the function is not case-insensitive).

Special NOTE: In the definition of PHP variables are best not to use the PHP system with functions or the same name as the system variables, easy to confuse, in addition, in the definition of PHP variables, in order to maintain a better coding habits, when the variable is composed of multiple words, the first word begins with the letter lowercase, the second word is capitalized ..., And so on, this is my personal advice.

Second, PHP variable declaration and assignment

Unlike programming languages such as C + +, PHP does not need to declare when using variables, you can use them when assigning values to variables, and use ' = ' when assigning values. Such as

 
  
  
  1. $test' Welcome to www.leapsoul.cn, here's the latest PHP introductory tutorial '

Iii. Types of PHP variables

Like other languages, PHP's variable types also support integers, strings, arrays, objects, and so on, except that other languages, such as C, need to declare the data type of the variable prior to using it, and the data type of the PHP variable does not need to be declared beforehand, and is determined when you assign it to him. Such as

 
  
  
  1. $leapsoul
  2. $leapsoularray();
  3. $leapsoul"Welcome to visit www.leapsoul.cn, this article mainly introduces the PHP variable and the constant learning"; //define PHP variables with a data type of string

In PHP, there is a special type of data-indeterminate variable, you can let us dynamically modify the variable name, before we say that the PHP variable is defined by the dollar symbol ($), if you add a dollar symbol ($) at the beginning, it becomes an indeterminate variable, that is

 
  
  
  1. $test' Leapsoul ';
  2. $ $test "Welcome to www.leapsoul.cn, this article mainly introduces PHP variables and constants of learning"

Equivalent to

 
  
  
  1. $leapsoul"Welcome to www.leapsoul.cn, this article mainly introduces PHP variables and constants of learning"

Iv. PHP variable functions

PHP variable functions are mainly used to determine the data type of variables, the existence of variables, the function of testing PHP variable type is:

GetType (): Returns the data type of the passed variable, or unknown type if it is not a standard data type, such as Integer, String, Array, object, and so on;

Settype (): Changes the data type of the passed variable, similar to coercion type conversion.

 
 
  1. $test ;
  2. $leapsoul = PHP variable and constant learning for the PHP Getting Started tutorial ;
  3. Echo GetType($test); //Output NULL
  4. Echo GetType($leapsoul); //Output variable type is string
  5. Settype ($leapsoul,"int"); //Set $leapsoul variable type to int
  6. Echo GetType($leapsoul); //Data variable type is integer

A function that determines whether a PHP variable is a specific data type is

Is_array (): Determine if the PHP variable type is an array type

Is_string (): Determine if PHP variable type is string type

Is_object (): Determines whether the PHP variable type is an object type

For more similar functions, you can refer to the PHP Help documentation

Functions to test the existence of PHP variables

The main use of isset and empty these two functions, the difference is that the Isset function is used to determine whether the variable exists, if it is true, otherwise return false, and the empty function is mainly used to determine whether the value of this variable is empty, or if the variable has no assignment, Returns true if NULL, otherwise false, which is useful when PHP form variables are submitted to background processing, in principle first using isset to determine the existence of variables, if any, the variable value of the required option can be judged by using the empty function.

Now that you have the knowledge of PHP variables in the PHP Getting Started tutorial, let's look at how PHP constants are used and defined

How PHP constants are defined and used

PHP constants are defined by the Define function, the constant name is generally capitalized, once the constant is defined, it can not be changed in the script process, usually in the development of large projects is, we generally put some commonly used functions, constants in a configuration file in advance, when used to include it in, This is also easy to manage.

 
  
  
  1. define ("INTRO","This code shows how PHP constants are defined and used");
  2. Echo

From the code example above, we can see that the difference between PHP constants and variables is that when a constant is used, there is no dollar sign in front of it, just use its name, and the variable is used with a dollar symbol.

More PHP system constants and environment variables you can see through the phpinfo () function. PHP environment variable $_server and system constants detailed description

At this point, PHP Introductory tutorial basic syntax of PHP variables and constants of the basics of the introduction is complete, in the next PHP introductory tutorial I will mainly introduce the PHP function definition and use, and incidentally introduced PHP variable scope of knowledge.

Original Address: http://www.leapsoul.cn/?p=427


http://www.bkjia.com/PHPjc/445792.html www.bkjia.com true http://www.bkjia.com/PHPjc/445792.html techarticle in a process of change, we call it constant, and the variable is a contiguous storage space with a name. The following is a description of the PHP variables and constants of the base ...

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