PHP基础语法

php基本语法
<html>
<body>
<?php
//输出语句——————注释方式1
echo "this is display by echo";
prinf "this is dispaly by prinf"
/*
  进行变量的定义------------注释方式2
*/
$name = "user";
$password = "userPassword";
$_note = "login";
echo $_note;
//并置运算符 (.) 用于把两个字符串值连接起来
echo $name . " " . $password;
//strlen()函数用于计算字符串的长度
echo strlen("hello word");
?>

/*strpos() 函数用于在字符串内检索一段字符串或一个字符。
如果在字符串中找到匹配,该函数会返回第一个匹配的位置。如果未找到匹配,则返回 FALSE
下面例子会输出hello中h的位置下标
*/
<?php
echo strpos("this is hello word ","hello");
?>
<body>
<html>
PHP中数组分为三种类型:数值数组、关联数组、多维数组
数值数组定义方式:
$names = array("Peter","Quagmire","Joe"); //自动分配ID键也可手动分配ID键
关联数组定义方式:
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
多维数组定义方式:
$families = array
(
  "Griffin"=>array
  (
  "Peter",
  "Lois",
  "Megan"
  ),
  "Quagmire"=>array
  (
  "Glenn"
  ),
  "Brown"=>array
  (
  "Cleveland",
  "Loretta",
  "Junior"
  )
);
foreach 语句用于循环遍历数组
//下面的例子示范了一个循环,这个循环可以输出给定数组的值:
<html>
<body>
<?php
$arr=array("one", "two", "three");
foreach ($arr as $value)
{
  echo "Value: " . $value . "<br />";
}
?>
</body>
</html>

PHP中创建函数
<html>
<body>
<?php
function writeMyName($name)
  {
  echo $name . " Liu.<br />";
  }
echo "My name is ";
writeMyName("小A");
echo "My name is ";
writeMyName("小B");
echo "My name is ";
writeMyName("小C");
?>
</body>
</html>
 

你可能感兴趣的:(PHP)