编程语言
首页 > 编程语言> > Week 1 —— 初涉PHP

Week 1 —— 初涉PHP

作者:互联网

Lab1.1 - Print Statements

我就捡着我觉得有话可说的地方记录了哈。

        <?php
        // put your code here
        echo 'Hello World!';
        echo'<br>'; //换行
        echo 'You can join strings '.'by using the dot'; //连接
        echo '<br>';
        echo 'John'.' '.'Smith';
        echo '<br>';
        $date = date('d/M/Y'); //日期函数
        echo $date;
        ?>

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


在同一个项目中建立多个PHP文件的方法:
1.
在这里插入图片描述
2.再把他的属性中的扩展改成php即可
在这里插入图片描述

Lab1.2 - Variables & Datatypes

        <?php
        // put your code here
        $num = 88;
        $name = "Wang haohua";
        $PI = 3.14;
        $success = true;
        echo $num." ".$name." ".$PI." ".$success." "."<br>";
        echo "$num $name $PI $success <br>";
        echo "My name is $name and my facourite number is $num<br>";
        echo "Today's date is ". date("d m Y");
        ?>

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

Lab1.3 - Test Datatypes

        <?php
        // put your code here
        $testing ;
        echo "is null?".is_null($testing);
        echo '<br/>';
        $testing = 5;
        echo "is an integer?".is_int($testing);
         echo '<br/>';
        $testing = 5.024;
        echo "is a double?".is_double($testing);
         echo '<br/>';
        $testing =true;
        echo "is a boolean?".is_bool($testing);
         echo '<br/>';
        $testing = array('apple','orange','pear');
        echo "is a array?".is_array($testing);
        echo '<br/>';
        echo 'is numeric?'.is_numeric($testing);
        echo '<br/>';
        echo 'is a resource?'.is_resource($testing);
        echo '<br/>';
        echo 'is an  array?'. is_array($testing);
        echo '<br/>';
        
        $undecided = 3.14;
        echo 'is'.$undecided." a double?". is_double($undecided)."<br/>";
        settype($undecided, "string");
        echo 'is'.$undecided." a string?". is_string($undecided)."<br/>";
        settype($undecided, "integer");
        echo 'is'.$undecided." an integer?". is_integer($undecided)."<br/>";
        settype($undecided, "double");
        echo 'is'.$undecided." a double?". is_double($undecided)."<br/>";
        settype($undecided, "bool");
        echo 'is'.$undecided." a boolean?". is_bool($undecided)."<br/>";
        
        $undecided = 3.14;
        $holder = (double)$undecided;
        echo 'is '.$holder." a double?". is_double($holder)."<br/>";
        $holder = (string)$undecided;
        echo 'is '.$holder." a string?". is_string($holder)."<br/>";
        $holder = (integer)$undecided;
        echo 'is '.$holder." an integer?". is_integer($holder)."<br/>";
        $holder = (double)$undecided;
        echo 'is '.$holder." a double?". is_double($holder)."<br/>";
        $holder = (boolean)$undecided;
        echo 'is '.$holder." a boolean?". is_bool($holder)."<br/>";
        echo '<hr/>';
        echo 'original variable type of $undecided: ';
        echo gettype($undecided);
        ?>

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

eg:

        <?php
        $a = 3.14;
        settype($a, "int"); // 法一
        echo $a."<br/>";
        $a = 3.14;
        echo (int)$a; //法二
        ?>

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

Lab1.4 - Operators

        <?php
        // put your code here
        $firstname = "John";
        $lastname = "Smith";
        $fullname = $firstname ." " . $lastname;
        echo $fullname . "<br/>";
        
        $a = 10;
        $b = 3;
        $c = $a + $b;
        echo $c." = ".$a." + ".$b."<br/>";
        $c = $a - $b;
        echo $c." = ".$a." - ".$b."<br/>";
        $c = $a * $b;
        echo $c." = ".$a." * ".$b."<br/>";
        $c = $a / $b;
        echo $c." = ".$a." / ".$b."<br/>";
        $c = $a % $b;
        echo $c." = ".$a." % ".$b."<br/>";
        
        $a++;
        $b--;
        echo '$a is '.$a.' after increment <br/>';
        echo '$b is '.$b.' after decrement <br/>';
        
        $c = $a > $b;
        echo $c."<br/>";
        
        $c = $a < $b;
        echo $c."<br/>";
        
        $c = $a == $b;
        echo $c."<br/>";
        
        $c = $a === $b;
        echo $c."<br/>";
        
        $c = $a <= $b;
        echo $c."<br/>";
        
        $c = $a >= $b;
        echo $c."<br/>";
        
        $a = true;
        $b = false;
         
        $c = $a || $b ;
        echo $c."<br/>";
        
        $c = $a && $b ;
        echo $c."<br/>";
        ?>

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

$a = ‘2’ ;
$b = 2 ;
$a != $b 是错的,因为数据类型不相同
$a !== $b 是对的,因为这个符合不完全等于

Lab1.5 - Progress Activity

        <?php
        // put your code here
        $ID = 1824100075;
        $Given_name = "haohua";
        $Family_name = "Wang";
        $Enrolled = true;
        $code = "VDIT";
        $code_name = " Diploma of Information Technology";
        $unit = "VIT1204";
        $point = 12.00;
        $cost = 2048.00;
        
        echo "Student ID : $ID <br/>";
        echo "Given Name: $Given_name <br/>";
        echo "Family Name: $Family_name <br/>";
        if ($Enrolled){
            echo 'Enrolled: Yes <br/>';
        } else {
            echo 'Enrolled: NO <br/>';
}
        echo "Course Code: $code <br/>";
        echo "Course Name: $code_name <br/>";
        echo "Unit of Study: $unit <br/>";
        echo "Credit points: ";
        printf("%.2f",$point);
        echo '<br/>';
        echo "Total Cost: $";
        printf("%.2f",$cost);
        echo '<br/>';
        echo 'Cost per credit point: $';
        $ans = sprintf("%.2f",$cost/$point);
        echo $ans;

运行结果:

在这里插入图片描述

梦里一声何处鸿 发布了54 篇原创文章 · 获赞 27 · 访问量 2628 私信 关注

标签:Week,初涉,testing,数据类型,echo,undecided,double,PHP,holder
来源: https://blog.csdn.net/Deam_swan_goose/article/details/104439446