Home Backend Development PHP Tutorial Introduction to comments, variables, arrays, constants, and function applications in PHP_PHP Tutorial

Introduction to comments, variables, arrays, constants, and function applications in PHP_PHP Tutorial

Jul 21, 2016 pm 03:14 PM
php and introduce function the difference variable constant application quotation marks array Comment

What is the difference between single quotes and double quotes in php?
1. As can be seen from the following, variables with double quotes are parsed and output, while variables with single quotes are not parsed.
2. The parsing speed of single quotes is faster than that of double quotes
3. For single quotes, there are only two escapes ',\
4. Except for the above two escape characters, everything else is the same output.
5. For double quotes, in addition to \, ', ", there are also t, r, n, etc. that can be escaped.
6. In addition to single quotes and double quotes used to declare string variables , and heredoc method
program code:
//$age = 22;
//$str1 = 'He is $age this year'; //''Output as is
//$str2 = "He is $age this year"; //""To parse the variables inside, output 22
//echo $str1,"
",$str2;
Comments in php
(1) Start with //.
(2) Start with #.
#This is the shell single-line comment style
(3) Multi-line There is a kind of comment that starts with /* and ends with */
Notes in PHP
1. In js, if a statement occupies a single line, there is no need to add a ; sign at the end of the line
2. But , in PHP, a semicolon must be added after each line
Although there is an exception in the last sentence of the entire PHP, it is strongly recommended to add
3. For pure PHP pages, ?> does not need to be written
Moreover, for files that are not run directly but are included in other pages, it is often recommended to end them without adding ?>
The pages included in this way will execute faster and faster
php Variables in
1. There are 8 types of PHP variables
2. Integer type, floating point type, Boolean type, string type, NULL type, array type, object type, resource type
3 . In js, declare variables with var variable name [= value]. To declare variables in php, directly variable name = value;
4. Variable naming convention in php
5. The name of the variable consists of "letters" ", underscores, numbers, and combinations. And numbers cannot begin
6. There is a '$' mark in front of variables in PHP
7. echo is not a function, but a grammatical structure.
8 . You can print out variables
9. When you want to print multiple variables, separate them with ','
10. Passing values ​​of variables
11. Passing by value and assignment
15. String type
Variables and constants
(1) Constants
Variables can be reassigned at any time
//$age = 22;
//$age = 23;
//echo "
",$age;
(2) What is the difference between variables and constants?
1. Their declaration methods are different
2. Once a constant is declared, Its value cannot be changed
define('PI',3.14);
PI =3.23; Syntax error
echo "
",PI;
3. Variables can be destroyed, and Once a constant is established, it cannot be destroyed
unset($age);
var_dump($age);
4. Constants cannot be logged out
unset(PI); //Syntax error, logout is not allowed
echo PI;
5. Variables have their own scope, and external variables cannot be accessed by default inside the function.
And constants, once defined, are defined either globally or within the function.
Can be accessed anywhere on the page.
(3) Naming conventions for variables and constants
1. The naming conventions for constants are the same as those for variables from a grammatical perspective.
A combination of letters, numbers, and underscores is allowed, and numbers cannot begin with
2. From a customary perspective: Generally, constants are in "uppercase"
//define('SF',342);
//echo SF; //The latest version has made constant names case-sensitive
(4) What are the allowed values ​​of constants?
1. Only scalar types (single types) can be assigned to constants;
2. Composite types, such as arrays and objects, cannot be assigned to a constant.
3. Resource type If assigned to a constant, it may cause some unpredictable errors.
The code is as follows:

Copy the code The code is as follows:

1 define('AGE' ,22);
2 define('HEI',343.234);
3 define('ROOT','D:/www');
4 define('LOCK',true);
5 define('NON',NULL);
6 echo AGE,HEI,ROOT;
7 var_dump(LOCK);
8 var_dump(NON);

Control structure in PHP
(1) Any program is inseparable from variables, expressions, and control structures
(2) In PHP, else if can be connected Writing is not allowed in js. We recommend standard writing, that is, esle if separation.
(3) In PHP, the scope of variables does not look out along the scope like in JS.
(4) In PHP, there is a special type of variable called super global variable. No matter you are in a function or inside a class, no matter how deep the code is packaged
you can access the variable.
php arrays and js arrays
(1) Two ways to create arrays in js
(2) In js, the index of an array always starts from 0 and increases one by one, with no gap in between
1 . var arr=new Array(1,2,3,4);
2. var arr= [1,2,3]
(3) Create array in php
1. But in php , the index of the array is very flexible
2. It can be a number or a string
3. It can even be a mixture of numbers and strings
4. If the index part specifies a numeric index
5. There is also a unit without a specified index
6. Then take the largest numeric index value that has appeared before in this unit and then +1 as its key value
php creates an array as follows:
Copy code The code is as follows:

$arr=array(1,2,3);
print_r($arr);
//============================================
$arr=array(10=>'Zhao','adfdssd'=>'Qian','Sun','name'=>'Zhang Sanfeng');
print_r($arr);

7. In PHP, how to reference the cell value of an array depends on the index
8. And the index is a numeric index
//echo $arr[10];
9. If It is a string index, and single quotes must be added. If no single quotes are added, it will be treated as a constant first
//define('name','adfdssd');
//echo $arr[name] ;
(4) Associative array and index array
1. The index may be a pure number, a string, or a mixed string + number
2. If the index is a pure number, It is called 'index array';
3. Otherwise it is called "associative array";
(5) The difference between functions in php and functions in js
1. In js, you can have more Declaring a function with the same name multiple times
2. But in a PHP page, a function with the same name cannot be used multiple times
3. In JS, function names are case-sensitive
4. In PHP, function names are not case-sensitive (Class methods are not distinguished)
5. In PHP, the number of parameters when calling a function must be consistent with the parameters of the declared function
6. In PHP functions, when the function is declared, a certain parameter can have " Default value"
Code display for all the above knowledge points
Copy the code The code is as follows:

//====================================== ============= Return to original location
//2. Integer type, floating point type, Boolean type, string type, NULL type
$age = 22;
$weight = 75.23;
$name ='Zhang San';
$money = false;
$house = null; //Equivalent to undiffed in js
echo $age,$weight, $money,
//============================================ ====== Return to the original place
//10. Value transfer of variables
$age =22;
$nian =$age;//Read the value of $age and assign it to $nian
$nian= 24;
echo $nian,'----',$age;
//================== ==============================
//11. Pass by reference, assignment and pass by value
$money =10000 ;
$credit = &$money; //Declare the $credit variable and point the credit pointer to the storage space of money
$credit = 5000;
echo $credit,'------- -',$money;
unset($credit);
echo $credit;
//======================== =========================
//String type
$str1 = 'hello';
$str2 = " world";
echo $str1,$str2,"
";
//======================== =========================
$age = 22;
function t(){
var_dump($age);
}
t();
define('HEI',88.63);
function s(){
var_dump(HEI);
}
s();
// Note: Functions in php cannot be declared repeatedly. Variables in functions are packaged more strictly and only work within the function. It will not run outside and work
//5. For the above situation, you can use variables or constants, but we choose constants.
//Reasons: The first is ROOt, which is often quoted
//The second is: if a variable is used, $ROOT ='a'; it is very likely that the value will be changed during multi-person development
// There are also disadvantages to using constants:
//Once a constant is defined, it will not be destroyed
//Constant is always internal and cannot be destroyed.
//================================================ ========
//In php, variable names are also variable.
$talk='hello';
$heat= 'kill you';
$love= 'love';
echo $love,"
";
$action = 'talk';
$t ='action';
echo $$$t;
//==================== ==================================
//Advance notice: not only variable names, but also function names can be Changeable, the class name is also variable.
////Constant names also need to be changed
define('PI',3.14);
define('HEI',342);
$cons= 'PI';
echo $cons,"
";
echo constant($cons); //constant is the name that treats the value of the variable as a constant, referencing the constant
//====== =============================================== Return to original position
//Control structure in php
//Any program is inseparable from variables, expressions, and control structures
if ,if/else,if/else if/ esle
$num =3;
if($num >2){
echo 'In php, 3 is also greater than 2',"
";
}
if($num >5){
echo '3 is greater than 5',"
";
}else{
echo '3 is not greater than 5';
}
//
if($nun==1){
echo 'Today is Monday';
}else if($num ==3){
echo 'Today is Wednesday';
}else{
echo 'not one, not two, not three';
}
////In php, else if can be written consecutively, but it is not allowed in js
//We recommend Standard writing, that is, esle if separated.
//================================================ ========
switch case statement
$num = 3;
switch($num){
case 1:
echo 'Today is Monday',"< br />";
break;
case 2:
echo 'Today is Tuesday',"
";
break;
case 3:
echo 'Today is Wednesday',"
";
break;
default:
echo 'Don't know';
break;
}
/*
Suppose someone has 100,000 in cash and needs to pay a fee every time he passes through an intersection.
The tariff rule is that when his cash is greater than 50,000, he needs to pay 5% in cash every time he passes through the intersection. If his cash is less than or equal to
= 50,000, he needs to pay 5,000 each time. Please write a program to calculate how many times this person can pass by. This intersection
*/
for ($m =100000,$num=0;$m>=5000;$num++){
if ($m>50000){
$m*= 0.98;
}else{
$m-=5000;
}
}
//==================== =======================
//while ,do/while
//Use while to print $1-9;
$i =1;
while($i<10){
echo $i++,"
";
}
$i=0;
while(++$ i<10){
echo $i,"
";
}
//==================== =======================
while(){}
$i=0;
while(++$i<10 ){
if($i==5){
break;
continue;
}
echo $i,"
";
}
////============================================
//Super global variables in php
$num =99;
function t(){
echo $num;
//}
////In this calling process, $num is null because $num
//// is not defined inside the function and in php, it does not search outside the scope like js.
t();
///======================================== ======
//In php, there is a special type of variables called super global variables.
//Whether you are in a function or inside a class, no matter how deep the code is packaged
//You can access this variable.
function a(){
echo $_GET['title'];
}
a();
///============= ==============================
//I want to use php to make a guestbook
//About logical operators
var age= 2||3;
alert(age);
var_dump($age);
//In php, logical operations return true/false
$age = 2 ||3;
var_dump($age);
$a = 3;
$b= 2;
if($a=9 || $b=1){
$ a +=1;
$b +=1;
}
echo $a,"
",$b;
//Function in php and js The difference between functions
//1. In js, you can declare a function with the same name multiple times
//But in a php page, you cannot declare a function with the same name multiple times
//2. In js, Function names are case-sensitive
//In php, function names are not case-sensitive (class methods are not case-sensitive either)
///================== ========================== Return
///3. In PHP, the number of parameters when calling a function must be the same as the number of parameters when declaring the function. The parameters are consistent
$a = 1;
$b = 2;
$c = 3;
function t($a,$b,$c){
echo $a+$b+ $c;
}
t(1,2,3);
t(1,2);
///================ ============================
//4. In PHP functions, when the function is declared, a certain parameter can have " Default value"
function t($a,$b,$c=0){
echo $a+$b+$c;
}
t(1,2);
? >

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326288.htmlTechArticleWhat is the difference between single quotes and double quotes in php? 1. As can be seen from the following, variables with double quotes are parsed and output, while variables with single quotes are not parsed. 2. The parsing speed of single quotes is faster than that of double quotes 3...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP's Current Status: A Look at Web Development Trends PHP's Current Status: A Look at Web Development Trends Apr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles