来点新手PHP学习心得
1,花括号“{}”可以像“[]”操作数组一样操作字符串,来获得指定位置的字符。
2,PHP标签“”在独立PHP脚本内可以不写结束标签,这是为了避免意外的空格导致输出而报错。可以用注释来标明脚本结束。
3,echo是语法结构,不是函数。后面跟多个字符串时用逗号“,”效率更好。
4,数组中,1、'1'、true为索引的时候都会强制转换为1。而'01'不会进行转换,会按照字符串处理。
5,将一个类的代码写在不同PHP标签内是不合法的,会报语法错误。而函数则没问题。
6,session与cookie的区别与关系。
- session保存在服务器上,cookie保存在客户浏览器上;
- session保存可以是硬盘上的文件、数据库、memcached,cookie可以保存到硬盘(持久cookie)和内存里(会话cookie);
- session_id传递方式有两种,一是cookie,二是get方式(可以通过 session.name 配置项来指定保存session_id的变量名称)。
7,获得当前时间戳用$_SERVER['REQUEST_TIME']代替time(),可以减少一次函数调用,效率更高。
8,检查字符串是否超过某长度可以用isset($str{n})的语法代替strlen()函数,例如:判断$a的长度是否超过5,可以用isset($a{5})来判断。这样效率更高。
9,header()函数过后要exit,否则后面代码还会执行。
10,大数组用引用传递,减少内存占用,用完就unset()。
11,数据库连接在使用的时候才建立,完全用完了记得关闭连接。
12,set_time_limit()的局限性。只能限制脚本本身的运行时间,对于外部执行的时间无法控制,例如:system()函数,流操作,数据库查询等。
13,abstract和interface的区别:
- abstract可以有非抽象方法,interface不行;
- abstract对方法的访问控制可以有protected,而interface必须为public;
- abstract只能被继承,当然一个类只能继承一个类,而一个类则可以实现多个interface。
14,echo,print,print_r,var_dump,var_export的区别:
- echo,print是语法结构,不是函数,而且只能显示基本类型,不能显示数组和对象,其他都是函数,可以显示数组和对象;
- echo 可以显示多个变量,用逗号隔开;
- print_r第二个参数可以决定是输出变量,还是将变量作为返回值;
- var_dump会打印变量的详细信息,例如长度和类型,而且可以传递多个变量作为参数;
- var_export返回的是合法PHP代码格式。
15,验证邮箱: filter_var($email, FILTER_VALIDATE_EMAIL);
16,获取文件扩展名的方法:
- 一,pathinfo($filename),取extension的值。
- 二,end(explode('.',$filename))。
17,文件锁定函数flock——的常量参数。
共享锁(读取操作)——LOCK_SH
独占锁(写入操作)——LOCK_EX
释放锁(无论共享还是独占)——LOCK_UN
防堵塞——LOCK_NB
可以通过fclose()函数释放锁定操作。
18,验证字符串是否是合法IP:
不用正则,直接用ip2long(),合法则返回数字,不合法则返回false。
19,PHP 5.3开始,可以使用__DIR__来获得当前脚本所在目录,不用再realpath(__FILE__)了。

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



Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

In iOS 17, Apple introduced several new privacy and security features to its mobile operating system, one of which is the ability to require two-step authentication for private browsing tabs in Safari. Here's how it works and how to turn it off. On an iPhone or iPad running iOS 17 or iPadOS 17, Apple's browser now requires Face ID/Touch ID authentication or a passcode if you have any Private Browsing tab open in Safari and then exit the session or app to access them again. In other words, if someone gets their hands on your iPhone or iPad while it's unlocked, they still won't be able to view your privacy without knowing your passcode

The famous activation script MAS2.2 version supports digital activation again. The method originated from @asdcorp and the team. The MAS author calls it HWID2. Download gatherosstate.exe (not original, modified) from https://github.com/massgravel/Microsoft-Activation-Scripts, run it with parameters, and generate GenuineTicket.xml. First take a look at the original method: gatherosstate.exePfn=xxxxxxx;DownlevelGenuineState=1 and then compare with the latest method: gatheros

Cookies on your computer are stored in specific locations on your browser, depending on the browser and operating system used: 1. Google Chrome, stored in C:\Users\YourUsername\AppData\Local\Google\Chrome\User Data\Default \Cookies etc.

Cookies are usually stored in the cookie folder of the browser. Cookie files in the browser are usually stored in binary or SQLite format. If you open the cookie file directly, you may see some garbled or unreadable content, so it is best to use Use the cookie management interface provided by your browser to view and manage cookies.

Session failure is usually caused by the session lifetime expiration or server shutdown. The solutions: 1. Extend the lifetime of the session; 2. Use persistent storage; 3. Use cookies; 4. Update the session asynchronously; 5. Use session management middleware.

Solution to the cross-domain problem of PHPSession In the development of front-end and back-end separation, cross-domain requests have become the norm. When dealing with cross-domain issues, we usually involve the use and management of sessions. However, due to browser origin policy restrictions, sessions cannot be shared by default across domains. In order to solve this problem, we need to use some techniques and methods to achieve cross-domain sharing of sessions. 1. The most common use of cookies to share sessions across domains

Cookies on the mobile phone are stored in the browser application of the mobile device: 1. On iOS devices, Cookies are stored in Settings -> Safari -> Advanced -> Website Data of the Safari browser; 2. On Android devices, Cookies Stored in Settings -> Site settings -> Cookies of Chrome browser, etc.
