Home > Backend Development > PHP Tutorial > PHP study notes one

PHP study notes one

不言
Release: 2023-03-24 12:02:01
Original
1773 people have browsed it

The content of this article is about PHP learning notes 1, which has a certain reference value. Now I share it with everyone. Friends in need can refer to it


Click to open the link on Learning PHP Notes.

1. PHP framework

<?php
//这是PHP程序代码
?>
Copy after login


2. Variables

(1) Naming rules

Variables start with the $ symbol Start, followed by the name of the variable

Variable names must start with a letter or underscore character

Variable names can only contain alphanumeric characters and underscores (A-z, 0-9 and _)

Variable names cannot contain spaces

Variable names are case-sensitive ($y and $Y are two different variables)



Note: PHP variables and statements are case-sensitive.


# (2) There is no statement to declare a variable, and the variable is created when it is assigned a value for the first time.

<?php
$txt="Hello world!";
$x=5;
$y=10.5;
?>
Copy after login


(3) PHP is a weakly typed language. PHP will automatically convert variables into the correct data type based on the value of the variable.


(4) Variable scope (four types): local, global, static, parameter



local: local variables, declared inside the PHP function, can only be accessed inside the function

global: global variables, variables defined outside all functions, have global effects Domain; to access a global variable in a function, you need to use the global keyword


<?php 
$x=5; // 全局变量 

function myTest() 
{ 
    $y=10; // 局部变量 
    echo "<p>测试函数内变量:<p>"; 
    echo "变量 x 为: $x"; 
    echo "<br>"; 
    echo "变量 y 为: $y"; 
}  //仅能输出$y的值,不能输出x的值,因为它是全局变量没有函数中使用global关键字

myTest(); 

echo "<p>测试函数外变量:<p>"; 
echo "变量 x 为: $x"; 
echo "<br>"; 
echo "变量 y 为: $y"; //仅能输出$x,不能输出$y
?>
Copy after login


<?php
$x=5;
$y=10;
 
function myTest()
{
    global $x,$y;
    $y=$x+$y;
/*等同于:
$GLOBALS[&#39;y&#39;]=$GLOBALS[&#39;x&#39;]+$GLOBALS[&#39;y&#39;];
*/}
 
myTest();
echo $y; // 输出 15
?>
Copy after login

PHP stores all global variables in In an array named $GLOBALS[index], index saves the variable name; this array can be accessed inside the function, or can be used directly to update global variables.



static: When you want the local variables of a function not to be deleted when the function is completed, you can use the static key Character. But it is still a local variable.

parameter: Parameters are called to functions through code and declared as part of the function.

<?php
function myTest($x)
{
    echo $x;
}
myTest(5);
?>
Copy after login


3. Echo statement and print statement

are both output statements.

echo can output one or more strings, with no return value, and the output speed is faster than print;

print only allows the output of one string, and the return value is always 1.

<?php
echo "<h2>PHP 很有趣!</h2>";//文字是标题格式
echo "Hello world!<br>";
echo "我要学 PHP!<br>";
echo "这是一个", "字符串,", "使用了", "多个", "参数。";//可以输出多个字符串
?>
Copy after login
<?php
print "<h2>PHP 很有趣!</h2>";
print "Hello world!<br>";
print "我要学习 PHP!";//只允许输出一个字符串
?>
Copy after login
<?php
$txt1="学习 PHP";
$txt2="RUNOOB.COM";
$cars=array("Volvo","BMW","Toyota");
/*使用print和echo都可以*/
print $txt1;
print "<br>";
print "在 $txt2 学习 PHP ";
print "<br>";
print "我车的品牌是 {$cars[0]}";
?>
Copy after login


4. Definition of string - PHP EOP

Usage rules:
must be followed by a semicolon

EOF It can be replaced by any other characters, just make sure that the end mark is consistent with the start mark.
The end mark must be on its own line, and it cannot be connected with any blanks or characters before and after.

The start mark can be without quotation marks or with single and double quotation marks. Without quotes and with double quotes are used to interpret embedded variables and escape symbols, with single quotes not to interpret



# #When the content has embedded quotes, there is no need to escape

<?php
$name="runoob";
$a= <<<EOF
    "abc"$name
    "123"
EOF;
// 结束需要独立一行且前后不能空格
echo $a;
?>
Copy after login

Note that it starts with the <<



5. Data type

String, Integer, Float, Boolean, Array, Object, NULL



(1) String string: can be placed in single or double quotes

(2) Integer integer type : Can be an integer or a negative number; three formats - decimal, hexadecimal (0x), octal (0)

<?php 
$x = 5985;
var_dump($x);//输出int(5985)
echo "<br>"; //换行
$x = -345; // 负数
var_dump($x);//int(-345)
echo "<br>"; 
$x = 0x8C; // 十六进制数
var_dump($x);//int(140)
echo "<br>";
$x = 047; // 八进制数
var_dump($x);//int(39)
?>
Copy after login


var_dump() function returns the data of the variable Type and value.

(3) Float floating point type: with decimals, or exponential form (e represents the power of 10)

(4) Boolean type




(5) Array array

<?php 
$cars=array("Volvo","BMW","Toyota");
var_dump($cars);
?>
Copy after login

Output:

array(3) { [0]=> string(5) "Volvo" [1]=> string(3) "BMW" [2]=> string(6) "Toyota"}



## (6) Object object: The object data type must be declared

First use the class keyword to declare the class object (properties, methods), define the data type in the class, and then Use data type

<?php
class Car
{
  var $color;
  function Car($color="green") {
    $this->color = $color;//this就是只想当前对象实例的指针,不指向任何其他对象或类
  }
  function what_color() {
    return $this->color;
  }
}
?>
Copy after login

in instantiation (7) NULL empty value: no value and no type




6. Constants

To set constants, you often use the define() function. The function syntax is:

bool define ( string $name , mixed $value [, bool $case_insensitive = false ] )
Copy after login



Among them, name is the name of the constant; value is the value of the constant; case_insensitive is optional. If TRUE, it is case-insensitive and the default is sensitive.

Constants default to global variables.



7. String operations

(1) Parallel operator. ——Convert two strings Values ​​are concatenated

<?php 
$txt1="Hello world!"; 
$txt2="What a nice day!"; 
echo $txt1 . " " . $txt2; 
?>
Copy after login


(2) strlen() function: Returns the string length (number of characters)

(3)strpos() 函数:用于在字符川内查找一个字符或一段指定文本,找到返回第一个匹配字符位置,未找到返回FALSE

<?php 
echo strpos("Hello world!","world"); 
?>
Copy after login


8、运算符

(1)

$x=10; $y=6; $a="hello";



$x/$y=1.6666666666667

var_dump(intp(10,3)); //输出int(3)



$a.=" world"; //$a="hello world"

==等于

===绝对等于,值和类型都相同

(2)数组运算

<?php
$x = array("a" => "red", "b" => "green"); 
$y = array("c" => "blue", "d" => "yellow"); 
$z = $x + $y; // $x 和 $y 数组合并
var_dump($z);
var_dump($x == $y);
var_dump($x === $y);
var_dump($x != $y);
var_dump($x <> $y);
var_dump($x !== $y);
?>
Copy after login


(3) Ternary operator: (expr1) ? (expr2) : (expr3)

When expr2=expr1, can be omitted as (expr1) ? : (expr3), can also mean (expr1) ?(expr3)

(4)优先级


<p style="margin-bottom: 7px;"><?php<br/>// 优先级: &&  >  =  >  and<br/>// 优先级: ||  >  =  >  or<br/> <br/>$a = 3;<br/>$b = false;<br/>$c = $a or $b;<br/>var_dump($c);          // 这里的 $c 为 int 值3,而不是 boolean 值 true<br/>$d = $a || $b;<br/>var_dump($d);          //这里的 $d 就是 boolean 值 true <br/>?><br/></p>
Copy after login




The above is the detailed content of PHP study notes one. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template