php PHP备忘单

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php PHP备忘单相关的知识,希望对你有一定的参考价值。

<?php
    if (isset($_GET["name"])) {
        print_r($_GET);
        echo htmlentities($_GET["name"]);
    }  
    if (isset($_POST["name"])) {
        print_r($_POST);
        echo htmlentities($_POST["name"]);
    } 
    if (isset($_REQUEST["name"])) {
        print_r($_REQUEST);
        echo htmlentities($_REQUEST["name"]);
    }   
    echo $_SERVER["QUERY_STRING"];
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>MY Website</title>
    </head>
    
    <body>
        <form action="get_post.php" method="post">
            <div>
                <label>Name</label><br>
                <input type="text" name="name" id="name">
            </div>
            <div>
                <label>Email</label><br>
                <input type="text" name="email" id="email">
            </div>
            <input type="submit" value="Submit">
        </form>
    </body>
</html>
<?php
    # $_SERVER SUPERGLOBAL

    // Create Server Array
    $server = [
        "Host Server Name" => $_SERVER["SERVER_NAME"],
        "Host Header" => $_SERVER["HTTP_HOST"],
        "Server Software" => $_SERVER["SERVER_SOFTWARE"],
        "Document Root" => $_SERVER["DOCUMENT_ROOT"],
        "Current Page" => $_SERVER["PHP_SELF"],
        "Script Name" => $_SERVER["SCRIPT_NAME"],
        "Absolute Path" => $_SERVER["SCRIPT_FILENAME"]
    ];

    // print_r($server);

    // Create Client Array
    $client = [
        "Client System Info" => $_SERVER["HTTP_USER_AGENT"],
        "Client IP" => $_SERVER["REMOTE_ADDR"],
        "Remote Port" => $_SERVER["REMOTE_PORT"]
    ];

    // print_r($client);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>System Info</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<body>
    <div class="container">
        <h1>Server & File Info</h1>
        <!-- PHP Shorthand Syntax -->
        <?php if($server) : ?>
            <ul class="list-group">
                <!-- PHP Shorthand Syntax -->
                <?php foreach($server as $key => $value) : ?>
                    <li class = "list-group-item">
                        <strong><?php echo $key; ?>: </strong>              
                        <?php echo $value; ?>
                    </li>
                <?php endforeach; ?>
            </ul>
        <?php endif; ?>

        <h1>Client Info</h1>
        <!-- PHP Shorthand Syntax -->
        <?php if($client) : ?>
            <ul class="list-group">
                <!-- PHP Shorthand Syntax -->
                <?php foreach($client as $key => $value) : ?>
                    <li class = "list-group-item">
                        <strong><?php echo $key; ?>: </strong>              
                        <?php echo $value; ?>
                    </li>
                <?php endforeach; ?>
            </ul>
        <?php endif; ?>
    </div>
</body>
</html>
<?php
    // Array - Variable that holds multiple values
    // 3 Types:
    // - Indexed
    // - Associative (Hashtable)
    // - Multi-dimensional

    function echoAsParagraph($textToEcho){
        echo "<p> $textToEcho </p>";
    }

    // Indexed Array
    $people = array("Kevin", "James", "Sarah");
    echoAsParagraph($people[1]);

    $ids = array(23, 55, 12);
    echoAsParagraph($ids[2]);

    $cars = ["Honda", "Toyota", "Ford"];
    echoAsParagraph($cars[1]);

    // Adding an element to an array
    $cars[3] = "Chevy";
    echoAsParagraph($cars[3]);

    // Another way of adding an element to an array
    $cars[] = "BMW";
    echoAsParagraph($cars[4]);

    echoAsParagraph(count($cars));
    echoAsParagraph(var_dump($cars));
    print_r($cars);

    // Associative Arrays
    $people = array("Brad" => 35, "Jose" => 32, "William" => 37);
    $ids = [22 => "Brad", 44 => "Jose", 63 => "William"];
    echoAsParagraph($people["Brad"]);
    echoAsParagraph($ids[22]);
    // Add to an Associative Array
    $people["Jill"] = 42;
    $ids[765] = "Jill";
    echoAsParagraph($people["Jill"]);
    echoAsParagraph($ids[765]);

    //Multi-Dimensional Arrays
    $cars = array(
        array("Honda", 20, 10),
        array("Toyata", 30, 10),
        array("Ford", 23, 5)
    );

    echoAsParagraph($cars[1][0]); // Toyota
    echoAsParagraph($cars[2][2]); // 5
?>
<?php
        function echoAsParagraph($textToEcho){
            echo "<p> $textToEcho </p>";
        }

        echoAsParagraph(date("d")); // Day
        echoAsParagraph(date("l")); // Week Day
        echoAsParagraph(date("m")); // Month
        echoAsParagraph(date("Y")); // Year
        echoAsParagraph(date("m/d/Y")); 
        echoAsParagraph(date("Y/m/d")); 
        echoAsParagraph(date("Y-m-d")); 

        date_default_timezone_set("CET");
        echoAsParagraph(date("h")); // Hour
        echoAsParagraph(date("i")); // Minutes
        echoAsParagraph(date("s")); // Seconds
        echoAsParagraph(date("a")); // AM or PM

        // Set Time Zone
        echoAsParagraph(date("h:i:sa"));

        // Working with Timestamps
        $timestamp = mktime(10, 14, 54, 9 , 19, 1981);
        echoAsParagraph($timestamp); 
        echoAsParagraph(date("m/d/Y h:i:sa", $timestamp)); 

        $timestamp2 = strtotime("7:00pm March 22 2016");
        echoAsParagraph($timestamp2); 
        echoAsParagraph(date("m/d/Y h:i:sa", $timestamp2)); 

        $timestamp3 = strtotime("tomorrow");
        echoAsParagraph(date("m/d/Y", $timestamp3));

        $timestamp4 = strtotime("next sunday");
        echoAsParagraph(date("m/d/Y", $timestamp4));

        $timestamp5 = strtotime("+2 Years");
        echoAsParagraph(date("m/d/Y", $timestamp5));
?>
<?php
    function echoAsParagraph($textToEcho){
        echo "<p> $textToEcho </p>";
    }

    // LOOPS - Execute Code a set number of times

    /* TYPES:
        - For
        - While
        - Do-While
        - ForEach
    */

    # For Loop
    for ($i=0; $i <= 10; $i++) { 
        echoAsParagraph($i);
    }

    # While-Loop
    $zahl=0;
    while ($zahl <= 10) {
        echoAsParagraph($zahl);
        $zahl++;
    }

    # Do-While Loop
    $zahl2=11;
    do {
        echoAsParagraph("I run at least once!");
    } while ($zahl2 < 10);

    #ForEach Loop
    $people = array("Brad", "Jose", "William");
    $assocs = array("Brad" => 34, "Jose" => 54, "William" => 76);

    // Loop through indexed arrays
    foreach ($people as $dude) {
        echoAsParagraph($dude);
    }

    // Loop through associative arrays
    foreach ($assocs as $assoc => $age) {
        echoAsParagraph($age);
    }
?>
<?php

    function echoAsParagraph($textToEcho){
        echo "<p> $textToEcho </p>";
    }

    # Comparison Operators
    /*
        ==
        ===
        <
        >
        <=
        >=
        !=
        !==
    */

    $num = 6;

    // IF - ELSEIF - ELSE

    if ($num == 5) {
        echoAsParagraph("5 passed");
    } elseif ($num == 6) {
        echoAsParagraph("6 passed");
    } else {
        echoAsParagraph("false");
    }

    // IF Nesting

    $num = 5;
    if($num > 4){
        if($num < 10){
            echoAsParagraph("Passed");
        }
    }

    // Conditionals ("&&", "||" and "XOR")

    if($num > 4 && $num < 10){
        echoAsParagraph("Passed");
    }

    // Switches

    $text = "Muh";

    switch ($text) {
        case 'Hello':
            echoAsParagraph("Hello!");
            break;
            
        case 'Muh':
        echoAsParagraph("Muh!");
        break;
        
        default:
            echoAsParagraph("Nothing!");
            break;
    }

?>
<?php
    function echoAsParagraph($textToEcho){
        echo "<p> $textToEcho </p>";
    }

    // Simple function without returning anything

    function simpleFunction(){
        echoAsParagraph("Hello World!");
    }

    simpleFunction();

    // Function that returns a sum and has optional parameters

    function simpleAddition($num1 = 2, $num2 = 3){
        return $num1 + $num2;
    }

    echoAsParagraph(simpleAddition(2, 4));
    echoAsParagraph(simpleAddition());

    // Passing Arguments as References

    $myNumber = 10;
    # Argument passed as value
    function addFive($num){
        $num += 5;        
    }

    addFive($myNumber);

    echoAsParagraph($myNumber);

    #Argument passed as reference
    function addTen(&$num){
        $num += 10;
    }

    addTen($myNumber);

    echoAsParagraph($myNumber);
?>
<?php
    // Single line comment

    /* 
    Multi-line comment
    */

    # Another single line comment

    // Print to Browser
    // echo "<p>Hello World!</p>";
    // echo phpversion();
    // phpinfo();
    // echo $_SERVER["HTTP_USER_AGENT"];

    define("GREETING", "Hello Everyone!");
    echo GREETING;
    
    // Variable types
    $string = "Hello World!"; // String
    $number1 = 1; // int
    $number2 = 1.2; // float
    $boolean = true; // boolean     
    // echo $string;

    // Number addition
    $num1 = 4;
    $num2 = 5;
    $sum = $num1 + $num2;
    // echo $sum;

    // String concatenation
    $string1 = "Hello";
    $string2 = "World";
    // $greeting = $string1 . " " . $string2;
    
    // String interpolation
    $greeting = "<p>$string1 $string2</p>";
    // echo $greeting;
?>
<?php
    echo "Hello World";
?>

以上是关于php PHP备忘单的主要内容,如果未能解决你的问题,请参考以下文章

php Drupal 8备忘单

php Laravel备忘单

markdown 使用php webdriver(facebook / webdriver)的备忘单。

php [php:accessor] setter / getter模式备忘录。 #PHP

php [php:error / exception] php的错误和异常备忘录。 #PHP

PHP设计模式之备忘录模式