在命令行中定义一个字符串:

<?php

$name="runoob";

$a= <<<EOF

"abc"$name

"123"

EOF;

// 结束需要独立一行且前后不能空格

echo $a;

?>

注意:

以 <<<EOF 开始标记开始,以 EOF 结束标记结束,结束标记必须顶头写,不能有缩进和空格,且在结束标记末尾要有分号。

php基本对象

php对象

<?php

class Car

{

var $color;

function __construct($color="green") {

$this->color = $color;

}

function what_color() {

return $this->color;

}

}

?>

php常量

语法定义:

bool define ( string $name , mixed $value [, bool $case_insensitive = false ] )

  • name:必选参数,常量名称,即标志符。
  • value:必选参数,常量的值。
  • case_insensitive :可选参数,如果设置为 TRUE,该常量则大小写不敏感。默认是大小写敏感的。

<?php

// 不区分大小写的常量名

define("GREETING", "欢迎访问 Runoob.com", true);

echo greeting; // 输出 "欢迎访问 Runoob.com"

?>

php字符串处理

字符串连接:

‘ . ’

<?php

$txt1="Hello world!";

$txt2="What a nice day!";

echo $txt1 . " " . $txt2;

?>

字符串求长度

<?php

echo strlen("Hello world!");

?>

字符串位置

<?php

echo strpos("Hello world!","world");

?>

php数据运算

PHP基础数据的使用

 

<?php

$x = array("a" => "red", "b" => "green");

$y = array("c" => "blue", "d" => "yellow");

$z = $x + $y; // $x 和 $y 数组合并

var_dump($z);

var_dump($x == $y);

var_dump($x === $y);

var_dump($x != $y);

var_dump($x <> $y);

var_dump($x !== $y);

?>

php数组

基础数据

<?php

$cars=array("Volvo","BMW","Toyota");

var_dump($cars);

?>

获取数组值

<?php

$cars=array("Volvo","BMW","Toyota");

echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";

?>

获取数组长度

<?php

$cars=array("Volvo","BMW","Toyota");

echo count($cars);

?>

遍历数组

<?php

$cars=array("Volvo","BMW","Toyota");

$arrlength=count($cars);

for($x=0;$x<$arrlength;$x++)

{

echo $cars[$x];

echo "<br>";

}

?>

关联数组

<?php<?

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

echo "Peter is " . $age['Peter'] . " years old.";

?>