Week 1 - initial PHP

Keywords: PHP Attribute

Lab1.1 - Print Statements

I picked up what I thought I had something to say and recorded it.

        <?php
        // put your code here
        echo 'Hello World!';
        echo'<br>'; //Line feed
        echo 'You can join strings '.'by using the dot'; //Connect
        echo '<br>';
        echo 'John'.' '.'Smith';
        echo '<br>';
        $date = date('d/M/Y'); //Date function
        echo $date;
        ?>

Operation result:

  • The newline code must be enclosed in quotation marks
  • Point operators are used to join. eg: 'John', 'Smith' are three strings. Two points are needed to connect them.
  • Before variables in PHP, add $. date() is a function that directly queries the date. Assign its output value to the date variable and then output.

How to create multiple PHP files in the same project:
1.

2. Change the extension in his attribute to 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");
        ?>

Operation result:

  • From the first two echo, we can see that the effect of spaces connected by point operators is the same as that of spaces in another string.
  • success is a boolean variable, so it is not displayed when output.
  • Date function can be used to specify the expression of date, as shown in the figure:

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);
        ?>

Operation result:

  • This lab wants to talk about data types. The function "is" data type (variable) "is used to determine the data type. If it is true, it will return 1. If it is false, it will no longer display the value.
  • GetType (variable) this is the return data type
  • Either a setType (variable, "set data type") or a (data type) variable is a cast.

eg:

        <?php
        $a = 3.14;
        settype($a, "int"); // Law 1
        echo $a."<br/>";
        $a = 3.14;
        echo (int)$a; //Law two
        ?>

Operation result:

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/>";
        ?>

Operation result:

  • In PHP, a boolean variable with a value of false does not display 0. I'm drunk.
  • ===It is absolute equal. Not only the values are required to be equal, but also the types of values are required to be the same. For example:
  • ! = = is not equal to all, if the values are the same but the data types are different, it is true, for example:

$a = '2' ;
$b = 2 ;
$a! = $B is wrong because the data types are different
$a! = = $B is right, because this match is not exactly equal to

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;

Operation result:

  • printf ("format", variable) this function controls the formatted output.
  • sprintf (format, variable) this function passes the formatted return value to the variable.
54 original articles published, praised 27 and visited 2623
Private letter follow

Posted by noodle on Sat, 22 Feb 2020 02:28:28 -0800