`
这些年
  • 浏览: 389175 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

简单入门

    博客分类:
  • php
 
阅读更多

1:安装

   wampServer 集成了:Apache 、 Mysql 、 PHP   

   你的代码放到 www directory 打开的目录下,进行http://localhost/文件名 即可

2:语法

<html> 
<body> 
<?php 
//This is a comment 
/* 
This is    php脚本 
*/ 
?> 
</body> 
</html>

 

 3:变量

<html>
<body>

<?php
echo "Hello World";           //echo  和 print 是输出命令
 
$var_name = "aaaaa";          //变量以字母或下划线,值决定变量类型
print $var_name;              //每条命令以;分隔
?>
</body>
</html>

 4:字符串操作

<?php
$txt1="Hello World";
$txt2="1234";
echo $txt1 . " " . $txt2;          // (.) 用于把两个字符串值连接起来
echo strlen("Hello world!");     //计算字符串的长度
echo strpos("Hello world!","world");        //返回第一个匹配的位置。如果未找到匹配,则返回 FALSE
?>

 5:运算符   

   算数运算符  :+ - * / % ++ --

 

   赋值运算符  :   += , -= ,*= , /= , %= ' .= , =

   比较运算符  :   == ,!= ,> ,< ,<= ,>=

   逻辑运算符 :   && ,|| ,!

6: if else

 

<html>
<body>

<?php
$d=date("D");                   //得到星期几
if ($d=="Fri")
  echo "Have a nice weekend!"; 
elseif ($d=="Sun")
  echo "Have a nice Sunday!"; 
else
  echo "Have a nice day!"; 
?>

</body>
</html>

 7:switch

<?php
switch ($x)
{
case 1:
  echo "Number 1";
  break;
case 2:
  echo "Number 2";
  break;
case 3:
  echo "Number 3";
  break;
default:
  echo "No number between 1 and 3";
}
?>

</body>
</html>

 8:数组

     数值数组 :带有数字 ID 键的数组
     关联数组 :数组中的每个 ID 键关联一个值
     多维数组 :包含一个或多个数组的数组
$names = array("Peter","Quagmire","Joe");     

$names[0] = "Peter";
$names[1] = "Quagmire";

$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);

$ages['Peter'] = "32";
$ages['Quagmire'] = "30";

$namee=array("aaa"=>array("a","b"));
 9:循环
<html>
<body>

<?php 
$i=1;
while($i<=5)
  {
  echo "The number is " . $i . "<br />";
  $i++;
  }


$i=0;
do
  {
  $i++;
  echo "The number is " . $i . "<br />";
  }
while ($i<5);


for ($i=1; $i<=5; $i++)
{
  echo "Hello World!<br />";
}

$arr=array("one", "two", "three");

foreach ($arr as $value)
{
  echo "Value: " . $value . "<br />";
}

foreach ($all_porduct_map as $k=>$v)
{
 echo $k.'=>'.$v.'<br />';
}
?>

</body>
</html>
 10:方法
<html>
<body>

<?php
function add($x,$y)
  {
  $total = $x + $y;
  return $total;
  }

echo "1 + 16 = " . add(1,16);
?>

</body>
</html>
 11:表单
<html>
<body>

<form action="welcome.php" method="post">    //向welcome.php发送请求
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

</body>
</html>
 welcome.php
<html>
<body>

Welcome <?php echo $_POST["name"]; ?>.<br />         //用于接收表单传过来的post数据
You are <?php echo $_POST["age"]; ?> years old.

</body>
</html>
 返回
Welcome John.
You are 28 years old.
 
$_REQUEST["name"] method="get|post"
$_POST["name"] method="post"
$_GET["name"] method="get"
 12:判断数据类型
gettype()
 13:string to array
$text = ‘item1,item2,item3,item4’;
$delimiter=’,’;
$itemList = explode($delimiter, $text);
 14:时间每循环一次加5分钟(传入的时间为string)
$time_str_time = date("Y-m-d H:i:s", strtotime($time_str_day) + $i * 300);
 15:得到数组大小
count($array)
 16:打印数据结构
var_dump ($a);
 17:转码(一定要保证php文件也是utf-8的编码)
iconv('utf-8', 'gbk', $content); //把utf-8 转成gbk

header('Content-type: text/html; charset=utf-8'); // 指定PHP源程序的编码
 18:得到时间
$day_num = 2;
$date_unix_start = strtotime("-$day_num day");   //前两天的时间
$date_str_start = date("Ymd", $date_unix_start);
echo $date_str_start;
$date_unix_end = strtotime("-0 day");//今天 的日期
$date_str_end = date("Ymd", $date_unix_end);
echo $date_str_end;
exit;                 //可以提前退出程序,调试用
 19:判断数组中的key是否存在
<?php
$a=array("a"=>"Dog","b"=>"Cat");
if (array_key_exists("a",$a))
	{
	echo "Key exists!";
	}
else
	{
	echo "Key does not exist!";
	}
?>
 
ksort(array)//数组按key排序
array_pop($array)  //删除最后一个元素
array_chunk($user_channels,100); //拆分数组,每个数组100个元素
$cards = array_merge($face, $numbered); //合并两个数组
$classScores = array_merge_recursive($class1, $class2); //合并数组,把key相同的放到子数组中
$stateMap = array_combine($abbreviations,$states); //第一个为key,第一个为值,建新数组,两数组元素相同不为空
array_push($array,'a','b');//追加元素
array_unshift($arr1,"a","b");//插入元素到首位
array_shift($arr1);//删除第一个元素
 
 20:全局变量
global $mailMessage;
 
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics