Declaration of PHP variables and naming rules for variables

Source: Internet
Author: User
Tags uppercase letter

Which of the following three types of writing is true?

In the PHP program, what is the naming rule for variables, which of the following three ways is correct?

The 1th kind:
<?php
$myname = ' AA ';
?>

The 2nd kind:
<?php
$myName = ' AA ';
?>

The 3rd kind:
<?php
$MyName = ' AA ';
?>


Experience sharing

Name, Age:

<?php
/*
* Variable name begins with $
* The name declaration of the variable must be meaningful
* Variables cannot be named at the beginning of a number, can be preceded by an underscore, and the $ and variable names cannot have spaces
* The operation symbol cannot appear in the variable name: subtraction
* Other programming language variables may not appear in the System keyword, but PHP can, because PHP has a $
*/
$name = "Beam bamboo";
$age = 20;
$php = 40;

Echo $age;
Variable name area is only partially case

<?php
/*
$int and $int are case-sensitive and Echo is case-insensitive
*/
$int = 10;
$INT = 20;

Echo $int, "<br>";
ECho $INT;

Hump-style naming: One Two three
<?php
$oneTwoThree = 10;
Variables are used to temporarily store worthwhile containers. These values can be numbers, text, or more complex permutations and combinations. is a simple tool for tracking almost all types of information.
PHP is a very weak type language. In most programming languages, a variable can hold only one type of data, and that type must be declared before using a variable, such as in the C language. In PHP, the type of the variable must be declared before the variable is used, for example, in the C language. In PHP, the type of a variable is usually not set by the programmer, or rather, based on the context used by the variable at runtime (that is, the value of the variable). PHP does not require a variable to be declared before it is used, and you create the variable the first time you assign a value to the first variable.
PHP's variable declarations start with a $ character, followed by uppercase and lowercase letters, numbers, and underscores, but cannot begin with a number.
<?php
$a = 100; Declare a variable A, give an integer 100
$b = "string"; Declares a variable B, giving the string
$c =true//Declare a variable C, giving the Boolean value true
$d = 99.99; Declare a variable d, give the float point 99.99
$key = $a; Declares a key variable and assigns the value of a variable to the
$a = $b = $c = $d = "value"//Declare multiple variables at the same time and give the same value
You can use the function () to release the specified variable, isset () function to detect whether the variable is set, empty () to check whether a variable is empty
Declaring multiple variables at the same time

<?php
$a = $b = $c = $d = 10;

echo $a;
echo "<br>";
Echo $b;
echo "<br>";
Echo $c;
echo "<br>";
Echo $d;
echo "<br>";
Var_dump (); Output variable type and content can be printed

<?php
Var_dump (10);
$a = 100;
Var_dump ($a);
The Isset () function detects whether a variable is set

<?php
$bool = Isset ($a);
Var_dump ($bool);

$a = 20;
$bool = Isset ($a);
Var_dump ($bool);
Unset (); Delete a variable

<?php
$a = 20;
unset ($a);
$bool = Isset ($a);
Var_dump ($bool);
Empty () count to check if a variable is empty and null to return True

<?php
$a = 20;
$b = ';
$c = 0;
$d = null;
$e = ';

Var_dump (Empty ($a));
Var_dump (Empty ($b));
Var_dump (Empty ($c));
Var_dump (Empty ($d));
Var_dump (Empty ($e));
Var_dump (Empty ($f));

Back to the beginning of the article, let's look at how to answer

Summarize

1, everyone likes no fixed pattern

2, the current PHP variable is not case-sensitive (it is said that PHP6 will be case-sensitive) so $myname= ' AA '; is written $myname= ' AA ', when the effect is inconvenient but less readable than $my _name= ' AA '; refreshing

3. For case-sensitive languages, it is generally recommended to use the Hungarian notation.

The variable name consists of a variable type and several words that begin with an uppercase letter representing the meaning of a variable
Like what
$myname = ' AA ';
Just write.
$sMyName = ' AA ';
and
$myname = 1;
Just write.
$iMyName = 1;

4. For case-insensitive languages, it is generally recommended that the variable name be composed of a number of words that indicate the meaning of the variable by the following connection

Like what
$myname = ' AA ';
Just write.
$my _name= ' AA ';

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.