php入门(1)
php入门(一)
一、基本数据类型
1,全局变量,局部变量及在函数内部使用全局变量
局部变量:可认为是函数内定义的变量。
全局变量:可认为是函数外定义的变量。
在函数内使用全局变量:
$my_var = "";
function func(){
echo $my_var; //报错,未定义变量$my_var;
global $my_var;
echo $my_var; //正常打印
}
copy-on-write 和 change on write(写时改变)
简单来说,在复制一个对象时并不是真的在内存中把原来对象的数据复制一份到另外一个地址,而是在新对象的内存映射表中指向同原对象相同的位置,并且把那块内存的Copy-On-Write位设为1. 在对这个对象执行读操作的时候,内存数据没有变动,直接执行就可以。在写的时候,才真正将原始对象复制一份到新的地址,修改新对象的内存映射表到这个新的位置,然后往这里写。
有一定经验的程序员一定知道,Copy-On-Write一定使用了“引用计数”,是的,必然有一个变量类似于RefCnt。当第一个类构造时,string的构造函数会根据传入的参数从堆上分配内存,当有其它类需要这块内存时,这个计数为自动累加,当有类析构时,这个计数会减一,直到最后一个类析构时,此时的RefCnt为1或是0,此时,程序才会真正的Free这块从堆上分配的内存。
深入理解PHP原理之变量分离/引用(Variables Separation)
参考链接:http://www.laruence.com/2008/09/19/520.html
2,可变变量
$var1 ='abc';
$$var1 ='def';
echo $abc; /**变量abc的值是def**/
这种就是php中的可变变量
3,intval函数
转化为整数。
intval(0.1);//0
intval(1.1);//1
intval("12",5);//十进制的7。注进制计算时,前面必须是字符串。
二、运算符与表达式
三、包含文件。include和require
include() 函数可获得指定文件中的所有文本,并把文本拷贝到使用 include 函数的文件中。include语法:
require() 函数与 include() 相同,不同的是它对错误的处理方式。
include() 函数会生成一个警告(但是脚本会继续执行),而 require() 函数会生成一个致命错误(fatal error)(在错误发生后脚本会停止执行)。
正因为在文件不存在或被重命名后脚本不会继续执行,因此我们推荐使用 require() 而不是 include()。
批注:
实际上常用的是require_once 'api/common.php';
四,文件处理
fopen 打开文件 $file = fopen("welcome.php", "r");
fgets 按行读 while( !feof($file) ){ echo $fgets( $file ); }
fgetc 按字符读 while( !feof($file) ){ echo $fgetc( $file ); }
fclose 关闭文件 fclose( $file );
五、常用函数
1,strip_tags(string, allow) 函数剥去 HTML、XML 以及 PHP 的标签。
2,strtoupper字符全部大写。
$name="phpfunction";echo $name;
$name = strtoupper($name);echo $name;
$name = strtoupper("php函数");echo $name;
输出结果:
phpfunctionPHPFUNCTIONPHP函数
3,is_object($v)
如果是对象返回true,否则返回false。
4,大小写转换
strtoupper($name) 转成大写

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
