search
HomeBackend DevelopmentPHP TutorialPHP and Mysql web application development core technologies - Part 1 Php basics - 2 PHP language introduction_PHP tutorial

The main topic is
. Variable expansion system in php strings
. More data types available in php
. Conversion between types
. Entering and using variables and constants
. How to build expressions in php and the operators needed to build expressions
. Using the control structures available in the language
.1 More introduction to input strings

Copy code The code is as follows:

$hour = 16;
$kilometres = 4;
$content = "cookie";
echo " 4pm in 24 hour time is {$hour}00 hours.
n";
echo There are {$kilometres }000m in {$kilometres}km.

The jar is now, indeed, full of ${content}s.

DONE;
?>

Output: 4pm in 24 hour time is 1600 hours.
There are 4000m in 4km.
The jar is now, indeed, full of cookies.
If you wish to To generate the exact character sequence {$ in the output, you need to use {$ to escape it.
.2 More introduction to data types
1. Array: Use the array method to declare an array. It gets a set of initial values ​​and returns an array object that holds all these values. By default, an integer name or key starting from 0 is assigned to the value
in the array. You can also specify new ones to be added. The index of the item. $frunit[120]="nespola";But you can also specify the key using a string value instead of the default number assigned to it.
$myfavourite=array("car"=>"ferrari","number"=>21,"city"=>"ouagadougou");

Array operator example name result
$a + $b union the union of $a and $b.
$a == $b equal TRUE if $a and $b have the same key/value pair.
$a === $b congruent TRUE if $a and $b have the same key/value pairs and are of the same order and type.
$a != $b does not equal TRUE if $a does not equal $b.
$a $b does not equal TRUE if $a does not equal $b.
$a !== $b

Copy code The code is as follows:

$a = array("a" => "apple", "b" => "banana");
$b = array("a" => "pear", "b" = > "strawberry", "c" => "cherry");
$c = $a + $b; // Union of $a and $b
echo "Union of $a and $b : n";
var_dump($c);
$c = $b + $a; // Union of $b and $a
echo "Union of $b and $a: n";
var_dump($c);
?>

After execution, this script will display:
Union of $a and $b: array(3) { ["a"]=> string(5) "apple" ["b"]=> string(6) "banana" ["c"]=> string(6) "cherry" } Union of $b and $a: array(3) { ["a"]=> string(4) "pear " ["b"]=> string(10) "strawberry" ["c"]=> string(6) "cherry" } 2.2.2 Objects will be used in object-oriented programming in the fourth unit. 2.2.3 Special types and values ​​NULL is a special type and value in PHP, which means "no value". It is null if it meets the following requirements: . They are set to the case-sensitive keyword null; . They have never been assigned a value .Clear them explicitly using the unset method. Resources: Sometimes, PHP needs to handle objects that do not necessarily come from PHP, such as handles to database or operating system objects. They are called special variables of resources.
.3 Casting
2.3.1 Basics
Implicit cast: The most common situations when encountering implicit cast are:
. Binary operation operator
. Boolean expressions and expression operator
. Specific methods that require strings - specific methods and operators, such as echoprint or string concatenation (.)
Show mandatory type Conversion: Add the type prefix enclosed in parentheses to the variable (or expression), and php will try to convert it for you.
(int)(interger)
(string)-convert to a text string
(object) - Convert to object
2.3.2 Special cast type conversion
Convert to integer
(int) 4.999
Note: null is always converted to the integer value 0.
Convert to float Points
(float)true=1.0
The result of converting an array, object or resource to a floating point value is undefined, do not attempt this new conversion or trust the result of such conversion
Convert to character String
can use the type conversion operator (string) or call strval to convert the variable into a string.
Boolean true is converted to string 1, false is converted to empty string ("")
null is converted to Empty string ('").
Convert to array
You can use type conversion (array) or the function arrayr to convert a variable or expression into an array
Null and other unset variables are converted to 0 Convert an empty array of elements to an object
You can use type conversion (object) to convert a variable or expression to an object. Create an object of type stdClass.
2.3.3 Useful cast function
is_type()
.is_integer,.is_float,.is_bool,is_null,.is_object. Returns a Boolean type, indicating whether a specific variable belongs to The appropriate type.
gettype() is a very useful routine that tells you what type PHP currently thinks a variable or expression is. It is not recommended to use this conversion function with two parameters: The variable to be converted and the type to be converted to, which represents a string
.4 Variables and constants
2.4.1 Define constants
In PHP programs, use the language structure define to define constants, and the constant names are not Start with the character $, and their values ​​can only be of specific types: integers, floats, strings and booleans
2.4.2 Variables by value and by reference
By default, most variables and all Constants are assigned by value. When the value of one variable is assigned to another variable, its value is copied. This method works for all types except objects
For object variables and resources, all the content copied is the handle of the underlying object or resource, but the underlying object of the operation is the same.
Another option for assigning the value of a variable to another variable is assignment by reference. Done with the & prefix.
$a=123;
$b=&$a;
2.4.3 Scope of variables
Function level variables, internally declared variables are only legal within this function.
Variables declared outside the function
Super global variables
2.4.4 Lifetime of variables
Whether executing the same script or different scripts, PHP will not remember anything between calls.
2.4.5 Predefined variables
php provides many predefined variables, which give information about the operating environment, most of which are super global arrays. For example:
$GLOBALS-it contains globally available variables within the executing script. References to all variables
$_SERVER - information about the environment surrounding the script
$_SESSION, $_COOKIE - it contains information about managing visitors and about storage methods called "cookies"
$_REQUEST - it contains $_post, $_GET and $_session arrays
$_ENV - It contains the environment variables of the process where the php language engine is located. The keys of the array are the names of the environment variables.
$php_errormsg - It saves the latest error message generated by the PHP language engine when executing the current script.
.5 Expressions and operators
2.5.1 Operators: Combination expressions
Assignment:
Arithmetic Operator
Example Name Result
-$a Negates the negative value of $a.
$a + $b adds the sum of $a and $b.
$a - $b subtracts the difference between $a and $b.
$a * $b multiplies the product of $a and $b.
$a / $b division The quotient of $a divided by $b.
$a % $b modulo the remainder of $a divided by $b.
Comparison Operators

Example Name Result
$a == $b equals TRUE if $a equals $b.
$a === $b is congruent TRUE if $a is equal to $b and they are of the same type. (Introduced in PHP 4)
$a != $b not equal TRUE if $a is not equal to $b.
$a $b not equal TRUE if $a is not equal to $b.
$a !== $b Not Congruent TRUE if $a is not equal to $b, or their types are different. (Introduced in PHP 4)
$a $a > $b is greater than TRUE if $a is strictly $b.
$a $a >= $b is greater than or equal to TRUE if $a is greater than or equal to $b.
Logical operator
Example Name Result
$a and $b And (logical AND) TRUE if $a and $b are both TRUE.
$a or $b Or (logical OR) TRUE if either $a or $b is TRUE.
$a xor $b Xor (logical exclusive OR) TRUE if either $a or $b is TRUE, but not both.
$a Not TRUE if $a is not TRUE.
$a&& $b And (logical AND) TRUE if both $a and $b are TRUE.
$a || $b Or (logical OR) TRUE if either $a or $b is TRUE.
Bitwise Operator

Operator Name Result
$a & $b And (bitwise AND) will set the bits in $a and $b that are both 1 to 1.
$a|| $b Or (bitwise OR) will set the bit in $a or $b that is 1 to 1.
xor ^ $b Xor (bitwise exclusive OR) will set different bits in $a and $b to 1.
Not $a Not (bitwise not) Sets bits in $a that are 0 to 1, and vice versa.
$a $a >> $b Shift right Moves the bits in $a to the right $b times (each move means "divide by 2").
String Operator
Concatenation Operator. It operates on two strings and returns a single string concatenating the two together.
Array Operator

Example Name Result
$a + $b union the union of $a and $b.
$a == $b equal TRUE if $a and $b have the same key/value pair.
$a === $b congruent TRUE if $a and $b have the same key/value pairs and are of the same order and type.
$a != $b is not equal TRUE if $a is not equal to $b.
$a $b does not equal TRUE if $a does not equal $b.
$a !== $b is not equal TRUE if $a is not equal to $b.
Other operators
Auto-increment and auto-decrement operators
$a=10;
$b=$a++; b=10 ,a=11;
$c=++$ a; c=12,a=12;
$d=$a--; d=12,a=11;
$e=--$a; e=10,a=10;
There is also an operator called @a which tells PHP to ignore the failure of a specific function call.
The last operator - shell command executor. For this purpose, the command needs to be enclosed in backticks (`) so that the command is passed to the shell for execution. However, this creates security.
2.5.2 The process of combining expressions and operators

Additional information on combined direction operators
Non-associative clone new clone and new
Left [array()
Non-associative + + -- Increment/decrement operator
non-associative~ - (int) (float) (string) (array) (object) (bool) @ type
non-associative instanceof type
right associative! Logical operation Operator
left*/% Arithmetic operator
left+-. Arithmetic operator and string operator
left> Bit operator
non-associative > 🎜>Left| Bit operator
Left&& Logical operator
Left|| Logical operator
Left? : Ternary operator
Right= += -= *= /= .= % = &= |= ^= >= assignment operator
left and logical operator
left xor logical operator
left or logical operator
left, multiple Used everywhere
.6 Control structure
2.6.1 if statement
1. if (expr)
statement
else
2. elseif/else if 2.6.2 switch statement



Copy code
The code is as follows: if ($a == 5):
echo "a equals 5";
echo "...";
elseif ($a == 6):
echo "a equals 6";
echo "!!!";
else:
echo "a is neither 5 nor 6";
endif;
?>


switch statement and a series of IF statements with the same expression resemblance. There are many situations where you need to compare the same variable (or expression) with many different values ​​and execute different code depending on which value it equals. This is exactly what the switch statement is for.


Copy code
The code is as follows: if ($i == 0) {
echo "i equals 0";
} elseif ($i == 1) {
echo "i equals 1";
} elseif ($i == 2) {
echo "i equals 2";
}
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
}
?>


2.6 .3 while/do ....while loop
while(expr)
block
do
block
while (expr);




Copy code
The code is as follows:

do {
if ($i echo "i is not big enough";
break;
}
$i *= $factor;
if ($i break;
}
echo "i is ok";
/* process i */
} while(0);
?>

2.6.4 for loop
for(expr1;expr2;expr3)
block
expr1: when Execute it once when a FOR loop is encountered. After the execution is completed, the loop iteration begins.
expr2: Calculate it before each iteration. If true, execute the code block.
expr3 - calculate it after each iteration
Copy code The code is as follows:

/* example 1 */
for ($i = 1; $i echo $i;
}
/* example 2 */
for ($i = 1; ; $i++) {
if ($i > 10) {
break;
}
echo $i;
}
/* example 3 */
$i = 1;
for (;;) {
if ($i > 10) {
break;
}
echo $i;
$i++;
}
/* example 4 */
for ($i = 1, $j = 0; $i ?>

2.6.5 foreach loop: used for specific types. More explained in Unit 5
2.6.6 Breaking Loops: break and continue

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323903.htmlTechArticleThe main topic is. Variable extension system in php string. More data types available in php. Types Conversion between. Entering and using variables and constants. How to build expressions in php and construct...
Statement
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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)