Using the built-in functions of PHP

PHP has a huge range of built-in functions designed to perform predefined tasks such as converting a string to upper case, getting the dimensions of an image, or sending an email. In this video I‘ll explain some of the basic features of working with functions. The first thing to note is that the function name is always followed by a pair of parenthesis. This function PHP version reports which version of PHP is running on your server.

All functions return a value.And unless you do something with it. It‘s simply thrown away. You can either display the value using echo or capture it in a variable. Sorry if use echo with this. And then load it in a browser. It tells me that the version I‘m currently running is 5.3.8. Now instead of displaying it immediately I could store it as a variable. So the version now contains the value returned by PHP version, so I could use this anywhere else I like in my script.

With PHP version there‘s no need to add anything between the parenthesis. But most functions expect you to pass one or more values as arguments. Now lets say that I‘m going to create a vairable called name and my caps lock has been stuck on and I‘ve spelt everything in upper case. PHP has a useful function which will convert uppercase to lowercase.

So if I lose echo and then STR to lower and pass it name as the argument. That should convert it to lowercase. just before I display it I‘m going to add a BR tag. And then I want to display name again.

Save that and load it into a browser. The first instance it‘s been converted to lower case, but when I display name again, it‘s still upper case, and this is a very important point. About variables being passed as arguments to functions. In the vast majority of cases, the original value stored in the variable remains unchanged. It‘s only the return value that has the changes reflected in it.

So if I want to save the changed value, I need to assign it either to a new variable or reassign it to a same variable. But having my name all in lowercase is not much better than having it all in uppercase. A really cool feature of php is the ability to pass a function as an argument to another function. Now, what I‘m going to do is use the, I‘m going to reassign the result of the return value to name, and use the function UC first, which converts the first character of a string to upper case.

And as the argument I‘m going to pass it, STR to lower and name. And then we‘ll just save that and see what happens in the browser. So what has happened is this PHP has executed this function first, converted name to lowercase and passed a return value as the argument to ucfirst which creates an initial cap. So in effect what has happened is that David has been converted to lowercase and then the lowercase version has been passed to ucfirst and the first letter has been capitalized.

Seeing this type of construction can be rather confusing when you‘re first beginning with PHP but that it‘s an extremely convenient shorthand and is used quite often. Both STR to lower and the UC first take only one arguments, but many functions take multiple arguments and some take optionable arguments. Here, I‘ve gotten input string, which might have come from, an online form. And it contains HTML tags.

And here There is some javascript. In this case, the javascript just says, Boo, but it could be sending me off to some malicious site, so obviously I wouldn‘t want to display this directly in my own website. So what I need to do is to be able to strip out the tags, and PHP very conveniently has a function which is called strip_tags. So let echo strip_tags, and pass it input. Now, strip tags accept one argument or two.

Let‘s try it with the second optional argument. So the first one will be input again, and the optional argument is allowable text, so I want to allow the p tag and the a tag. Just need the Opening tags as a string there. Close the parentheses. And let‘s save it, and see what happens in the browser. Now in both cases, the body of that Javascript has been left intact. But the script tags are no longer there, so the JavaScript won‘t be executed. Only the body of the script is displayed.

But in the second one, the link is still there, and if we just go into Source View, you can see that there are no html tags at all in the first example. The second one has preserved the

tags and tag, and we‘ve still got the "href" which will take you to the URL. I‘ve been able to use "input" twice here because the original value hasn‘t been changed.

And I haven‘t captured this as the return value. This is the normal behavior. Most functions don‘t change the original value of any variables passed some of these arguments. But there are some exceptions. Let‘s just see one here. I‘ve got an array which is not in alphabetical order. And down here, I‘ve used the sort function and asked it as its argument the characters array, and this will actually change the original array.

So if we view that in the browser, the array has been reordered in alphabetical order and there is another Function, which is called rsort, which reverses the order of the array. So if we just refresh the browser, it‘s now in reverse alphabetical order. I mentioned that functions always have a return value. And you might be wondering, well, what‘s the return value for sort and rsort? It‘s actually true or false depending on whether the sort was successful or not. So it‘s important to know the type of return value to expect from a built in function.

The best way to find out is to consult the excellent online PHP manual. The manual also describes which arguments a function expects. And whether any of them are optional. In the vast majority of cases, the original value of variables used as arguments remains unchanged. So you need to display the return value immediately, or capture it in a variable.

时间: 2024-11-06 07:11:38

Using the built-in functions of PHP的相关文章

Parallax

常听说牛人都是jquery插件用得很好的人. 现在有了github,英文过关的话什么好东西下不到啊,再不用去浏览那些抄来抄去骗人看广告的垃圾网站了. 扯远点,本人有严重的熊猫眼,所以用一种叫倦眼充电棒的遮瑕笔..然后我在浏览benefit官网的时候,发现有个页面做得很不错,使用了一个视动差插件:skrollr.js lovecss的博主也介绍过这个插件. 它自定义了html标签的attributes,只要使用这些属性,就可以在页面的绝对高度上设置“锚点”. “锚点”上支持一些已经封装好的效果,比

Writing a Kernel in C++

*:first-child { margin-top: 0 !important; } .markdown-body>*:last-child { margin-bottom: 0 !important; } .markdown-body .anchor { position: absolute; top: 0; bottom: 0; left: 0; display: block; padding-right: 6px; padding-left: 30px; margin-left: -30

从erlang时间函数说到时间校正体系

很多人会注意到这个问题,erlang提供了2个时间函数,erlang:now() 和 os:timestamp().用法一样,都是返回当前的时间.具体时间是从1970年1月1日零时算起,到现在经过的时间,结果为{MegaSecs, Secs, MicroSecs}. 这两个函数有什么区别? os:timestamp() 获取到的时间为操作系统的时间,不做任何修正:而erlang:now(),每次获取都会确保生成了唯一的时间,就是说,erlang:now()在实现上对时间做了一个校正,每次都生成一

Oracle Advanced Pricing White Papers

Oracle Order Management - Version 11.5.10.0 and later Oracle Advanced Pricing - Version 11.5.10 and later Oracle Purchasing - Version 11.5.10 and later Information in this document applies to any platform. ABSTRACT This document provides links to the

An introduction to parsing text in Haskell with Parsec

Parsec makes parsing text very easy in Haskell. I write this as much for myself as for anyone else to have a tutorial and reference which starts from the ground up and works through how each function can be used with examples all the way. First off,

(转载)虚幻引擎3--13掌握虚幻技术UnrealScript 接口

第十三章 – 接口 什么是接口? 接口示例 USB 电脑鼠标 电源插座 编程说明 定义接口 声明和定义 接口继承 实现接口 为什么使用接口? 结束语 指南 13.1 – 指南针, 第一部分:指南针接口 指南 13.2 –指南针, 第二部分:指南针类的实现 指南 13.3 –指南针, 第三部分: 测试指南针, 第一部分 指南 13.4 – 小地图, 第一部分:       MU_MINIMAP 类 指南 13.5 – 小地图, 第二部分: MINIMAPGAME 类 指南 13.6 – 小地图,

Chapter 1 - 初识Python,人人都爱列表

这是Head First Python 第一章的笔记,主要是Python的基础知识和列表的相关内容. 基础知识 BIF:built in functions,Python的内置函数,例如print(),help(),len()等.在实现某些功能前,首先考虑是否有BIF可用. BIF属于__builtins__命名空间:但是在执行脚本时并不需要导入此空间,因为这些BIF可以被自动导入__main__的命名空间. 来自为知笔记(Wiz)

Convert String to Long

问题: Given a string, write a routine that converts the string to a long, without using the built in functions that would do this. Describe what (if any) limitations the code has. 代码: 1 /* 2 * Author: Min Li 3 * Discussion: 4 * 1. Return 0 when input i

IOS开发—Using UIGestureRecognizer with Swift Tutoria

Update note: This tutorial was fully updated for iOS 8 and Swift by Caroline Begbie. Original post by Ray Wenderlich. Update 12/10/14: Updated for Xcode 6.1.1. If you need to detect gestures in your app, such as taps, pinches, pans, or rotations, it'

[Hive-Tutorial] Type System 数据类型

数据类型Type System Hive supports primitive and complex data types, as described below. See Hive Data Types for additional information. Hive支持原生和复杂数据类型. Primitive Types 原生数据类型 Types are associated with the columns in the tables. The following Primitive t