Week 1 - started his PHP

Lab1.1 - Print Statements

I think I have something to say picking up where records Kazakhstan.

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

operation result:
Here Insert Picture Description

  • The code must use the quotation marks that lead live line feed
  • The dot operator is used to connect to. eg: 'John', '', 'Smith' is three strings. They even need to take up two points.
  • Front PHP variables to add $, date () function is a direct query date, his output value to a date variable and then output.

Method for establishing multiple PHP files in the same project:
1.
Here Insert Picture Description
2. then his attributes can be extended into php
Here Insert Picture Description

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:
Here Insert Picture Description

  • Seen by the first two echo: linked by a dot operator and a further effect of space directly hit the string is the same space.
  • success is a Boolean variable, so when output is not displayed.
  • Predetermined function can be expressed with the date of the date, as shown:
    Here Insert Picture Description

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:
Here Insert Picture Description

  • The lab would like to say is the data type. "The is_ data type (variable)" This function is used to determine the data type, returns 1 if true, should no longer display the value of the false.
  • GetType (variable) This is the return data type
  • setType (variable, "Data type"), or "(data type) variable" are cast.

eg:

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

operation result:
Here Insert Picture Description

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:
Here Insert Picture Description

  • In PHP echo a Boolean variable is false does not actually display 0, drunk.
  • === it is absolutely equal, not only requires equal values, even value have the same type, such as:
    Here Insert Picture Description
  • ! == is not identical to, if the values ​​are the same but different data types is true, for example:

A = $ '2';
$ B = 2;
! $ A $ = B is wrong, because the data type is not the same
! $ a == $ b is, because this is not exactly in line with equal

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:

Here Insert Picture Description

  • printf ( "format", variable) This function can be controlled formatted output.
  • sprintf ( "format", variable) This function returns the value to the variable transmission format.
Published 54 original articles · won praise 27 · views 2609

Guess you like

Origin blog.csdn.net/Deam_swan_goose/article/details/104439446