编程语言之PHP

时间:2021-10-28 01:29:00

PHP语法集锦:

输出语句:

<?php
$a="Hellow world!"
echo $a;
?>

Hellow world!

数组:

<?php
$cars=array("Volvo","BMW","Toyota");
$arrlength=count($cars);
for($x=0;$x<$arrlength;$x++)
{
echo $cars[$x];
echo "<br>";
}
?>

Volvo
BMW
Toyota

关联数组:

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>

Peter is 35 years old.

定义常量:

<?php
// 区分大小写的常量名
define("GREETING", "欢迎访问 Runoob.com");
echo GREETING;
echo '<br>';
echo greeting;
// 不区分大小写的常量名
define("GREETING", "欢迎访问 Runoob.com", true);
echo greeting;
?>

“欢迎访问 Runoob.com”
“欢迎访问 Runoob.com”

整除:

<?php
echo intdiv(10,3);
?>

3

获取字符的ascall码:

<?php
$a=date('H');
echo $a;
?>

19

数组排序:
sort为升序,rsort为降序

<?php
$cars=array("Volvo","BMW","Toyota");
sort($cars);
?>

<?php
$numbers=array(4,6,2,22,11);
sort($numbers);
?>

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
asort($age);
?>

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
ksort($age);
?>

Array
(
[0] => Volvo
[1] => Toyota
[2] => BMW
)
Array
(
[0] => 2
[1] => 4
[2] => 6
[3] => 11
[4] => 22
)
Array
(
[Peter] => 35
[Ben] => 37
[Joe] => 43
)
Array
(
[Ben] => 37
[Joe] => 43
[Peter] => 35
)

超级全局变量:
http://www.runoob.com/php/php-superglobals.html

函数定义:

<?php
function a($b,$c){
echo $b . " " . $c;
}
a("aaa","bbbb");
?>

aaa bbb

魔术变量:

<?php
echo '这是第 " ' . __LINE__ . ' " 行';
echo '</br>';
echo '该文件位于 " ' . __FILE__ . ' " ';
echo '</br>';
echo '该文件位于 " ' . __DIR__ . ' " ';
echo '</br>';
function test() {
echo '函数名为:' . __FUNCTION__ ;
}
echo '</br>';
test();
echo '</br>';
namespace MyProject;
echo '命名空间为:"', __NAMESPACE__, '"';
?>

这是第 “ 2 ” 行
该文件位于 “ E:\wamp\www\test\index.php ”
该文件位于 “ E:\wamp\www\test ”
函数名为:test
命名空间为:”MyProject”

<?php
namespace MyProject {
const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */ }
}
namespace {
session_start();
$a = MyProject\connect();
echo MyProject\Connection::start();
}
?>

注意:
  命名空间语句必须在首行。

<?php 
trait T{
public function t_method(){
echo "this is t_method<br>";
}
}
class C{
use T;
public function c_method(){
echo "this is c_method<br>";
}
}
$cls= new C();
$cls->t_method();
//通过类调用
$cls->c_method();
//静态调用
T::t_method();
?>

this is t_method
this is c_method
this is t_method

htmlspecialchars的使用:

<?php
$str = "This is some <b>bold</b> text.";
echo htmlspecialchars($str);
echo $str;
?>

This is some <b>bold</b> text.
This is some bold text.

注意: htmlspecialchars()函数是不解析成html代码

数组读取:

 <form action="test.php" method="post">
<select multiple="multiple" name="q[]">
<option value="a">一</option>
<option value="b">二</option>
<option value="c">三</option>
</select>
<input type="submit" value="提交">
</form>

<?php
$q=isset($_POST['q'])?$_POST['q']:'';
echo $_POST['q'];
if(is_array($q)){
$sites=array('c','b','a'=>"我是第三个");
foreach ($q as $val){
echo $val;
echo $sites[$val] . PHP_EOL;
}
}
?>

注意:
 option中的value对应foreach中的$val值,多选框中选中第一个即a,则会输出数组中的第三个元素即“我是第三个”

单引号和双引号的区别:

  单引号不显示变量值,双引号显示变量值。