PHP之重要函数
积少成多——作者:zccst
8 , string base64_encode(string data);
本函数将字符串以 MIME BASE64 编码。此编码方式可以让中文字或者图片也能在网络上顺利传输。在 BASE64 编码后的字符串只包含英文字母大小写、阿拉伯数字、加号与反斜线,共 64 个基本字符,不包含其它特殊的字符,因而才取名 BASE64。编码后的字符串比原来的字符串长度再加 1/3 左右。更多的 BASE64 编码信息可以参考 RFC2045 文件之 6.8 节。
7,similar_text
similar_text() 函数计算两个字符串的匹配字符的数目,也可以计算两个字符串的相似度(以百分比计)。
参数 描述
string1 必需。规定要比较的第一个字符串。
string2 必需。规定要比较的第二个字符串。
percent 可选。规定供存储百分比相似度的变量名。
similar_text("Hello World","Hello Peter",$percent);echo $percent;
输出:63.6363636364
6,spl_autoload_register('autoloader');
function autoloader($className){ if(file_exists(dirname(__FILE__)."/$className.php")){ include dirname(__FILE__)."/$className.php"; if(class_exists($className,false)){ return true; } } return false;}
批注:
bool class_exists ( string $class_name [, bool $autoload = true ] )
第二个参数:Whether or not to call __autoload by default.
只有当第二个参数为false时,才能不开启autoload,以节省开支。
__autoload机制:
参阅另一篇文章:php autoload机制。
5,addslashes()和stripslashes()
addslashes() 函数在指定的预定义字符前添加反斜杠。 这些预定义字符是: 单引号 (') 双引号 (") 反斜杠 (\) NULL
$str = "Is your name O'reilly?";// Outputs: Is your name O\'reilly?echo addslashes($str);
stripslashes() 函数删除由 addslashes() 函数添加的反斜杠
4,get_magic_quotes_gpc()
int get_magic_quotes_gpc ( void )
Returns the current configuration setting of magic_quotes_gpc
Keep in mind that attempting to set magic_quotes_gpc at runtime will not work.
For more information about magic_quotes, see this security section.
批注:取得 PHP 环境变量 magic_quotes_gpc(GPC, Get/Post/Cookie) 的值。返回 0 表示关闭本功能;返回 1 表示本功能打开。
当 magic_quotes_gpc 打开时,所有的 ' (单引号), " (双引号), \ (反斜线) and 空字符会自动转为含有反斜线的转义字符。
当magic_quotes_gpc = On时,系统会自动处理单引号等问题,用不用addslashes()和stripslashes()都没关系,但是如果添加数据时用了addslashes(),那么显示数据时必须要stripslashes()
当magic_quotes_gpc = Off时,系统不会处理单引号等问题,所以插入数据时必须要使用addslashes(),显示数据时则不需要使用stripslashes()。
既然有了分析,做程序时要怎么办呢?根据以上两种情况,可得:
不管magic_quotes_gpc是On还是Off,咱添加数据时都用addslashes(),当On时,必须使用stripslashes(),Off时则不能用stripslashes()。
如何判断On还是Off呢?用get_magic_quotes_gpc()。
最后举例:
//1,存入数据库$Content=addslashes(”这里面是数据,不管有没单引号或者还是变量”);//插入数据到数据库,代码省略//2,从数据库取$Content=”从数据库读取的数据”;if(get_magic_quotes_gpc()){ $Content=stripslashes($Content); }echo $Content;/************ 例子2 **************/echo get_magic_quotes_gpc(); // 1echo $_POST['lastname']; // O\'reillyecho addslashes($_POST['lastname']); // O\\\'reillyif (get_magic_quotes_gpc()) { $lastname = stripslashes($_POST['lastname']);}else { $lastname = $_POST['lastname'];}// If using MySQL$lastname = mysql_real_escape_string($lastname);echo $lastname; // O\'reilly$sql = "INSERT INTO lastnames (lastname) VALUES ('$lastname')";
3,get_file_contents($fileName)
把文件读入一个字符串
file_get_contents() 函数是用来将文件的内容读入到一个字符串中的首选方法。
file_put_contents(string $filename , string $data [, int $flags [, resource $context ]] )
将一个字符串写入文件
2,str_replace(array('\r\n'), array("\n"), $str)
1,strip_tags($v)
原型:string strip_tags ( string $str [, string $allowable_tags ] )
解读:This function tries to return a string with all NUL bytes, HTML and PHP tags stripped from a given str. It uses the same tag stripping state machine as the fgetss() function.
举例:
$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';echo strip_tags($text);echo "\n";// Allow <p> and <a>echo strip_tags($text, '<p><a>');
输出:
Test paragraph. Other text
Test paragraph.
Other text
PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP is not dead. 1) The PHP community actively solves performance and security issues, and PHP7.x improves performance. 2) PHP is suitable for modern web development and is widely used in large websites. 3) PHP is easy to learn and the server performs well, but the type system is not as strict as static languages. 4) PHP is still important in the fields of content management and e-commerce, and the ecosystem continues to evolve. 5) Optimize performance through OPcache and APC, and use OOP and design patterns to improve code quality.

PHP and Python have their own advantages and disadvantages, and the choice depends on the project requirements. 1) PHP is suitable for web development, easy to learn, rich community resources, but the syntax is not modern enough, and performance and security need to be paid attention to. 2) Python is suitable for data science and machine learning, with concise syntax and easy to learn, but there are bottlenecks in execution speed and memory management.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor