PHP Starter Tutorials for PHP variables and constant learning

Source: Internet
Author: User
Tags define array definition empty functions variables php form types of functions

Last month I specifically introduced the PHP introductory tutorial on the basic syntax of PHP introductory learning, mainly introduced several commonly used PHP tags, the composition of PHP statements, PHP comments, and so on, today's PHP introductory tutorial we mainly learn PHP basic syntax PHP variables and constants of the basic knowledge.

For the introduction of PHP variables to learn, this introductory tutorial is divided into the following parts: How to identify PHP variables, PHP variable How to declare, how to assign the PHP variable, PHP variable type introduction, commonly used PHP variable function introduction.

Introductory Learning for PHP constants, mainly describes the definition of PHP constants and how to use.

how to identify a PHP variable

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, can't start with a number, remember in PHP, variables are case-sensitive (PHP system with the exception of the function is case-insensitive).

  Special Reminder : In the definition of PHP variables, it is best not to use the PHP system with functions or the same name with the system variables, easy to confuse, in addition to the definition of PHP variables, in order to maintain a better coding habits, when the variable consists of more than one word when the first word, the beginning The second word starts with the letter uppercase ..., and so on, which is my personal suggestion.

Second,PHP variable declaration and assignment

and C + + and other programming languages, PHP in the use of variables, do not need to be declared in advance, when you assign a value to the variable can be used, assign value when using ' = '. Such as

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

Iii.Types of PHP variables

As with other languages, the variable type of PHP also supports integral type, strings, arrays, objects, and so on, the difference is in other languages, such as the C language, you need to declare the data type of the variable before using the variable, and the data type of the PHP variable does not need to be stated beforehand, when you assign a value to him has been determined. Such as

1
2
3
$leapsoul = 1; Defines the data type of a PHP variable as an integral type
$leapsoul = Array ();//The data type that defines the PHP variable is an array
$leapsoul = "Welcome to visit www.leapsoul.cn, this article mainly introduces PHP's introductory tutorial PHP variables and constant learning";//define PHP variable data type as String

In PHP, there is a special type of data-indefinite variable, which allows us to dynamically modify the name of the variable, before we said that the definition of PHP variables in dollar ($) Start, if you add a dollar ($) at the beginning, it becomes an indefinite variable, that is

1
2
$test = ' Leapsoul ';
$ $test = "Welcome to visit www.leapsoul.cn, this article mainly introduces PHP's introductory tutorial PHP variables and constant learning";

Equal to

1
$leapsoul = "Welcome to visit www.leapsoul.cn, this article mainly introduces PHP's introductory tutorial PHP variables and constant learning";

Four,PHP variable function

PHP variable functions are mainly used to determine the variables of the data type, the existence of variables to judge, test PHP variable types of functions are:

  GetType (): Returns the data type of the variable passed over, and returns the unknown type if it is not a standard data type, such as an integer, a string, an array, an object, and so on;

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

1
2
3
4
5
6
7
8
9
10
11
$test;

$leapsoul = "PHP starter tutorial PHP variables and constant learning";

Echo GetType ($test);//Output NULL

Echo GetType ($leapsoul);//output variable type string

Settype ($leapsoul, "int");/set $leapsoul variable type int

Echo GetType ($leapsoul);//data variable type integer

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

Is_array (): Determines whether the PHP variable type is an array type

Is_string (): Determines whether the PHP variable type is a string type

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

More similar functions you can refer to the Help documentation for PHP

  A function to test the existence of PHP variables

The main use of the isset and empty functions, the difference is that the Isset function is used to determine whether the variable exists, if the existence of return true, otherwise return false, and the empty function is mainly used to determine whether the value of the variable is null, or that the variable is not assigned, Returns true if NULL, otherwise returns FALSE, these two functions are very useful when PHP form variables are submitted to the background processing, in principle, using isset to judge the existence of the variable, if the variable exists, the empty function can be used to determine the variable value of the required option if necessary.

So here's the knowledge of PHP variables in the introductory PHP tutorial, so let's look at how PHP constants are used and defined

  How PHP constants define and use

The PHP constants are defined by the Define function. Constant names are typically used in uppercase letters, and once constants are defined, they cannot be changed in the script process, usually in the development of large projects, we typically put some commonly used functions, constants in a configuration file, included in the use, This is also easy to manage.

1
2
3
Define ("INTRO", "This code shows how PHP constants for PHP introductory tutorials are defined and used");

Echo INTRO;

From the code example above, we can see that the difference between a PHP constant and a variable is that when using a constant, it has no dollar sign, it just needs to use its name, and the variable is used with a dollar sign.

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

So far, the basics of PHP variables and constants in the basic syntax of the PHP starter tutorial are covered, and in the next introductory PHP tutorial I will introduce the definition and use of PHP functions, and introduce the scope of PHP variables in passing.

  Note : PHP Web Development Tutorials-leapsoul.cn Copyright, reproduced in the form of links to indicate the original source and this statement, thank you.







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.