PHP基础

界定PHP代码:

  1. 默认语法以<?php开头,以?>结束,如下:
    <h3> Welcome!</h3>
    
        <?php
           print "<p> This is a PHP example.</p>";
         ?>
    <p> Some static information found here...</p>

  2. 脚本<script>
    <script language="php">
        print "This is another PHP example.";
    </script>
  3. 短标记  省略php引用。<?内容 ?> 。用使用这个特性,需要启用PHP的short_open_tag指令。一般情况下会使用print或echo语句来显示信息。启用短标记后,可以省略,使用短路语法(=)的输出形式:
    <?="This is another PHP example."; ?>
    <? print "This is another PHP example."; ?>
    <?php print "This is another PHP example."; ?>

    以上三种形式是等价的。

在一个页面中,PHP代码与非PHP代码可以多次交替出现。前面块中声明的所有变量都能被“记住”,能由后面的块使用。

<html>
  <head>
     <title><?php echo "Welcome to my site!"; ?></title>
  </head>
  <body>
     <?php 
        $date= "Jun 26, 2015";
     ?>
     <h3>Today's date is <?=$date;?></h3> //短路语法
  </body>
</html>

注释

三种注释:1 双斜线//开头  ;2 shell语法 #开头;3. /*       */

输出

  1. print() 

            boolean print(argument)以下都是print()语句:

<?php
       print("<p>I love the summertime.</p>");
       print "<p>I love the summertime.</p>";
       $season="summertime";
       print "<p>I love the $season.</p>";
       print "<p>I love the ".$season.".</p>";
     ?>

最后一种里面的点号.是拼接操作符。

程序输出

I love the summertime.

I love the summertime.

I love the summertime.

I love the summertime.

      2. echo()

              void echo(string argument1 [, ... string argumentN ] )

        echo()语句与print()类似,有两点不同。一,echo()不能用在复杂表达式中,因为它返回void,而print()返回一个boolean值。二,echo()能输出多个字符串。

<?php
       $heavyweight = "Lennox Lewis";
       $lightweight = "Floyd Mayweather";
       echo $heavyweight, "and", $lightweight, "are great fighters.";
     ?>

输出

Lennox LewisandFloyd Mayweatherare great fighters.

echo()与print()在功能上可以互换,echo()运行速度稍微快一些,因为它没有返回值,而print()要返回一个boolean值。


     3    printf() 格式化(format)输出

           boolean printf( string format [, mixed args])

           %2$ 表示参数位于参数列表的第二个位置,%3$表示参数位于参数列表的第三个位置。但是$在format字符串中必须转义,不转义仅仅表示美元符号。因此需要转义为 %2$, %3$ 。

    4    sprintf()

           string sprintf( string format [, mixed arguments] )

           sprintf()函数的功能与 printf()相同,但它将输出指派到字符串。

<html>
  <head>
     <title><?php echo "Welcome to my site!"; ?></title>
  </head>
  <body>
     <?php 
        $date= "Jun 26, 2015";
     ?>
     <h3>Today's date is <?=$date;?></h3>
     <? print "This is a PHP example."; ?>
     <? print "<p>This is a PHP example.</p>"; ?>
     <?php
       print("<p>I love the summertime.</p>");
       print "<p>I love the summertime.</p>";
       $season="summertime";
       print "<p>I love the $season.</p>";
       print "<p>I love the ".$season.".</p>";
       
       printf("<p>The %2$s likes to %1$s", "bark.</p>", "dog");
       $cost=sprintf("$%01.2f",43.2);
       print $cost
     ?>
     <?php
       $heavyweight = "Lennox Lewis";
       $lightweight = "Floyd Mayweather";
       echo  "<p>".$heavyweight, "and", $lightweight, "are great fighters.</p>";
     ?>
  </body>
</html>

任何数据类型都可以转换为对象。结果是,该变量成为了对象的一个属性,该属性名为scalar:

<?php  
       $model="toyota";
       $new_obj=(object) $model;
       print $new_obj->scalar;
?>

 字符串参与四则运算,如果字符串以整数或者浮点数开头,就使用这个值;如果以非数值开头,则值为0。

 $total="455.5fire engines";
       $incoming ="100ab";
       $total= $total/$incoming;
       print $total;

引用赋值:在等于符号后面加一个&符号或者将&符号放在所引用变量的前面

<?php
     $value1="Hello";
     $value2=& $value1;
     $value3=  &$value1;
     $value2="Goodbye";
     echo '$value1=',$value1,' ',"$value2=",$value2."<br/>";
?>
$value1=Goodbye $value2=Goodbye

单引号的字符串,里面都是字符串。而双引号的字符串,变量前面要加上转义字符,否则变量被当作变量处理。

 

原文地址:https://www.cnblogs.com/ly01/p/4603065.html