search

PHP study notes one

Apr 19, 2018 pm 02:25 PM
phpstudynotes

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程序代码
?>


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;
?>


(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
?>


<?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
?>

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);
?>


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 id="PHP-nbsp-很有趣">PHP 很有趣!</h2>";//文字是标题格式
echo "Hello world!<br>";
echo "我要学 PHP!<br>";
echo "这是一个", "字符串,", "使用了", "多个", "参数。";//可以输出多个字符串
?>
<?php
print "<h2 id="PHP-nbsp-很有趣">PHP 很有趣!</h2>";
print "Hello world!<br>";
print "我要学习 PHP!";//只允许输出一个字符串
?>
<?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]}";
?>


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;
?>

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)
?>


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);
?>

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;
  }
}
?>

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 ] )



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; 
?>


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

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

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


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);
?>


(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>




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

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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 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.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

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

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools