学习PHP基本语法

echo 和 print 语句

<?php
$x = 1;
echo $x;//可以使用echo或者echo()
echo "asd","adasd","dsad";//用,隔开多个字符串,echo 输出的速度比 print 快
$list = array("1233","1215",1);
echo $list[1];
echo "{$list[2]}";//必须加“”
echo "<br>";
print $x;//可以使用print或者print()
print $list[1];
print "{$list[0]}";//必须加“”
?>

EOF语句(定义字符串)

<?php
   //定义在EOF内部的都是字符串,不需要“”包裹,EOF前后不能有其他字符或空格,最后一条EOF必须
   //独占一行且定格写
   $test = <<<EOF
   asdassd
   fdfd
   1222
EOF;
   $test1 = <<<EOF
   $test
   adsd
   asfaf
EOF;
print $test;
print $test1;
?>

数据类型

//定义字符串用引号括起来
  $string = "字符串";
  //整型变量可以用十进制,十六进制,八进制表示
  $x = 5656; //十进制
  $y = 012;//八进制
  $z = 0x8f;//十六进制
  //浮点数
  $a = 3.1415;
  $b = 3.14e-2;
  //布尔型
  $c = true;
  $d = false;
  //数组
  $list = array("内容","内容");
  //var_dump函数是可以用来输出变量的值并且返回变量的类型
  var_dump($string);
  echo "<br>";
  var_dump($x);
  echo "<br>";
  var_dump($y);
  echo "<br>";
  var_dump($z);
  echo "<br>";
  var_dump($a);
  echo "<br>";
  var_dump($b);
  echo "<br>";
  var_dump($c);
  echo "<br>";
  var_dump($d);
  echo "<br>";
  var_dump($list);
?>

运行如下
在这里插入图片描述

类型比较

<?php
   /*
    * 类型比较
    * 1.用 == 比较时,不比较类型只比较值
    * 2.用 === 比较时,除了比较值也比较类型
    * 3.返回布尔值,true或false
    */
   if(1=='1'){
       print "yes1";
   }else{
       print "no1";
   }
   echo "<br>";
   if(1==='1')
       print "yes2";
   else
       print "no2";
?>

字符串查找,拼接

<?php
   //字符串变量的连接,用"."连接
   $string1 = "hello";
   $string2 = "world";
   $string3 = $string1." ".$string2;
   print $string3;
   echo "<br>";
   //strlen()函数可返回字符串的长度
   echo strlen($string1);
   echo "<br>";
   //strpos()可查找指定字符或字符串在字符串中的第一次出现位置,找到返回位置,失败返回FALSE
   echo strpos($string3,'o');
   echo strpos($string3,'t');
?>

运行结果如下
在这里插入图片描述

数组排列

<?php
   //对数组进行输出和排列
   $list = array(6,5,4,3,2,1);
   $l = array('a','c','b');
   sort($list); //对数字大小升序
   sort($l); //对字母升序
   print_r ($list); //使用print_r()来输出数组
   echo "<br>";
   print_r($l);
   //降序排列并输出
   echo "<br>";
   $list1 = array(1,2,3,4,5,6);
   rsort($list1);
   print_r($list1);
   //asort()对数组的键值进行升序排列
   $list2 = array("a"=>'1','b'=>'3','c'=>'2');
   asort($list2);
   echo "<br>";
   print_r($list2);
   //ksort()对数组的键进行升序排列,字母顺序
   $list3 = array("c"=>'1','a'=>'3','b'=>'2');
   asort($list3);
   echo "<br>";
   print_r($list3)
   /*
    * arsort和krsort是对数组进行降序排列,与上同理
    */
?>

运行结果如下
在这里插入图片描述

for循环的练习

<?php
   //用PHP实现冒泡排序,升序
  $a = array(5,6,4,7,2,31,8,92,2,100);
  for($i=0;$i<count($a)-1;$i++){
      for ($j=0;$j<count($a)-$i-1;$j++){
          if($a[$j]>$a[$j+1]){
              $t = $a[$j];
              $a[$j] = $a[$j+1];
              $a[$j+1]=$t;
          }
      }
  }
  print_r($a);
?>

运行结果如下:
在这里插入图片描述

魔术常量

<?php
  //魔术常量__LINE__,返回文件中的当前行号
  echo "这是第 ".__LINE__." 行"."<br>";

  /*
   * 魔术常量__FILE__,返回文件的完整路径和文件名
   * 魔术常量__DIR__,也返回文件的路径但不显示文件名
   */
  echo "文件位于: ".__FILE__."<br>";
  echo "文件位于: ".__DIR__."<br>";

  /*
   * 魔术常量__FUNCTION__,返回函数名字
   * 魔术常量__CLASS__,返回类名
   * 魔术常量__METHOD__,返回函数名并返回所在的类名
   */
  class test{
      function F(){
          echo "函数名称为: ".__FUNCTION__."<br>";
          echo "类名为: ".__CLASS__."<br>";
          echo "类名为: ".__METHOD__."<br>";
      }
  }
  $t = new test();
  $t->F();

?>

运行代码如下:
在这里插入图片描述

面向对象

构造方法的使用

<?php
  /*
   *面向对象,类用class关键字定义
   * 类的变量用public声明
   */
  class test1{
    //成员变量
    public $x;
    public $y;
    //成员函数
    function test1($t,$p){
      $this->x=$t;
      $this->y=$p;
    }
    function pt(){
      echo __CLASS__;
    }
  }
  $test = new test1(100,100);
  $test->pt();
  echo "<br>";
  echo $test->x."<br>",$test->y."<br>";

?>

运行代码如下:
在这里插入图片描述

表单的建立

html建立表单

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>表单处理</title>
</head>
<body>
<form action="index.php" method="post">
    姓名:<input type="text" name="name">
    年龄:<input type="text" name="age">
    <input type="submit" value="提交">
</form>
</body>
</html>

在这里插入图片描述
php接收数据

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
欢迎<?php echo $_POST["name"]; ?>!<br>
你的年龄是<?php echo $_POST["age"]; ?>岁
</body>
</html>

在这里插入图片描述

发布了9 篇原创文章 · 获赞 0 · 访问量 163

猜你喜欢

转载自blog.csdn.net/weixin_46176911/article/details/103983254