Centralt innehåll

1. Grundläggande PHP

När vi utvecklar och lär oss så vill vi ha så mycket information om alla fel som det bara går. I drift däremot vill man dölja vissa utskrifter.

Följande kod ska ALLTID ligga överst i PHP-filen för att få så mycket information som möjligt om olika fel.

<?php error_reporting(-1); //Report all type of errors ini_set('display_errors', 1); //Display all errors  ini_set('output_buffering', 0); //Do not buffer outputs, write directly ?>


Gör övning 1

2. Variabler

Variabler börjar med ett $ tecken.
Variabler skapas när de tilldelas ett värde.

<?php
    error_reporting(-1);             //Report all type of errors
    ini_set('display_errors', 1);    //Display all errors 
    ini_set('output_buffering', 0);  //Do not buffer outputs, write directly 
?>

<html>
<head>
    <meta charset="utf-8" />
    <title>Example 1</title>
</head>
<body>
    <?php
        $myText = "Welcome to PHP!";
        echo $myText;
    ?>
</body>
</html>

Variabler får en typ beroende på värdet de tilldelas.
Med funktionen var_dump får man mer information om variabeln.

<?php
    error_reporting(-1);             //Report all type of errors
    ini_set('display_errors', 1);    //Display all errors 
    ini_set('output_buffering', 0);  //Do not buffer outputs, write directly 
?>

<html>
<head>
    <meta charset="utf-8" />
    <title>Example 2</title>
</head>  
<body>
    <?php
        $number = 123;
        $decimal = 3.14;
        $myText = "Hi";
        $state = true;
        $nothing = null;

        echo "<pre>";
        var_dump($number);
        var_dump($decimal);
        var_dump($myText);
        var_dump($state);
        var_dump($nothing);
        echo "</pre>";
    ?>
</body>
</html>

2.1 Konkatenering av strängar

Konkateneringen är hur två eller flera strängar adderas (sammanfogas).
Se också exempel på kommentarstecknen // som gäller resten av raden och /* .... */ som kan användas för flera rader av kommentarer.

<?php
    error_reporting(-1);             //Report all type of errors
    ini_set('display_errors', 1);    //Display all errors 
    ini_set('output_buffering', 0);  //Do not buffer outputs, write directly 
?>

<html>
<head>
    <meta charset="utf-8" />
    <title>Example 3</title>
</head>
<body>
    <?php
        /*
          This example shows how to add two or
          more strings, that is concatenate strings.
        */
        $a = "Donald";
        $a = $a . " Duck";
        echo $a;    //$a is equal to "Donald Duck"
        echo "<br/>";

        $b = "Donald ";
        $b .= "Duck";    //Same as $b = $b . "Duck"
        echo $b;
    ?>
</body>
</html>

2.2 Enkla eller dubbla citattecken

<?php
    error_reporting(-1);             //Report all type of errors
    ini_set('display_errors', 1);    //Display all errors 
    ini_set('output_buffering', 0);  //Do not buffer outputs, write directly 
?>

<html>
<head>
    <meta charset="utf-8" />
    <title>Example 4</title>
</head>
<body>
    <?php
        $sum = 1 + 6;
        echo "The sum is $sum";    // Outputs: The sum is 7
        echo "<br/>";
        echo 'The sum is $sum';    // Outputs: The sum is $sum
    ?>
</body>
</html>

Vad blir utskriften i nedanstående kod? Tänk efter och gissa FÖRE du kör koden.

<?php
    error_reporting(-1);             //Report all type of errors
    ini_set('display_errors', 1);    //Display all errors 
    ini_set('output_buffering', 0);  //Do not buffer outputs, write directly 
?>

<html>
<head>
    <meta charset="utf-8" />
    <title>Example 5</title>
</head>
<body>
    <?php
        //Different variables
        $myText = "Welcome to PHP!";
        $number = 100;
        $someText = "times";
		
        echo "$myText $number $someText";
        echo "<br/>";
        echo '$myText $number $someText';
        echo "<br/>";
        echo $myText." ".$number." ".$someText;
    ?>
</body>
</html>

3. if-satsen

<?php
    error_reporting(-1);             //Report all type of errors
    ini_set('display_errors', 1);    //Display all errors 
    ini_set('output_buffering', 0);  //Do not buffer outputs, write directly 
?>

<html>
<head>
    <meta charset="utf-8" />
    <title>Example 6</title>
</head>
<body>
    <?php
        $variable = 77;
        $buu = true;
        
        if ($variable == 77) 
        {
            echo "The variable is 77";
        }
        else 
        {
            echo "The variable is not 77";
        }
		
        if ($buu) 
        {
            echo "<br/>BUU";
        }
        else 
        {
            echo "<br/><br/>not buu!";
        }
    ?>
</body>
</html>

En if-sats kan ha en eller flera elseif-grenar, men maximalt en else-gren.

<?php
    error_reporting(-1);             //Report all type of errors
    ini_set('display_errors', 1);    //Display all errors 
    ini_set('output_buffering', 0);  //Do not buffer outputs, write directly 
?>

<html>
<head>
    <meta charset="utf-8" />
    <title>Example 7</title>
</head>
<body>
    <?php
        $a = 50;
        if ($a != 100) 
        {
            echo "$a is not equal to 100 <br/>";
        }
	
        $b = 10;
        if ($a == $b) 
        {
            echo "$a equals $b <br/>";
        }
        else 
        {
            echo "$a is not equal to $b <br/>";
        }
	
        $difference = $a - $b;
        $quotient = $a / $b;
        $product = $a * $b;
        if ($product >= 100) 
        {
            echo "$product is equal to or over 100";
        }
        elseif ($product > 50) 
        {
            echo "$product is less than 100 but over 50";
        }
        elseif ($product >= 10) 
        {
            echo "$product is between 10 and 50";
        }
        else 
        {
            echo "$product is less than 10";
        }
    ?>
</body>
</html>

En if-sats med tre likhetstecken === innebär att man kontrollerar om värdet OCH datatypen på variablerna är lika.

<?php
    error_reporting(-1);             //Report all type of errors
    ini_set('display_errors', 1);    //Display all errors 
    ini_set('output_buffering', 0);  //Do not buffer outputs, write directly 
?>

<html>
<head>
    <meta charset="utf-8" />
    <title>Example 8</title>
</head>
<body>
    <?php
        $number1 = "77";
        if ($number1 === 77)  // OBS! 3 likhetstecken
        {
            echo "The variable is 77";
        }
        else 
        {
            echo "The variable is not 77";
        }
        echo "<br/>";
	
        $number2 = "77";
        if ($number2 == 77) 
        {
            echo "The variable is 77";
        }
        else 
        {
           echo "The variable is not 77";
        }
    ?>
</body>
</html>

4. while-loopen

Villkoret kontrolleras före loopen.

<?php
    error_reporting(-1);             //Report all type of errors
    ini_set('display_errors', 1);    //Display all errors 
    ini_set('output_buffering', 0);  //Do not buffer outputs, write directly 
?>

<html>
<head>
    <meta charset="utf-8" />
    <title>Example 10</title>
</head>
<body>
    <?php
        $num = 10;
        while ($num >= 4)
        {
           echo "$num <br/>";
           //$num = $num - 1;     
           //$num -= 1;     
           $num--;      
        }   
        echo "Ready";
    ?>
</body>
</html>			

5. for-loopen

Upprepar satser ett visst antal gånger.

<?php
    error_reporting(-1);             //Report all type of errors
    ini_set('display_errors', 1);    //Display all errors 
    ini_set('output_buffering', 0);  //Do not buffer outputs, write directly 
?>

<html>
<head>
    <meta charset="utf-8" />
    <title>Example 12</title>
</head>
<body>
    <?php
        for ($i = 1; $i <= 12; $i++) 
        {
            echo "Hi number $i <br/>";
        }
    ?>
</body>
</html>


Gör övning 2 & 3


Begrepp

error_reporting: Funktion för att ställa in hur mycket fel som ska visas.

Konkateneringen: Två eller flera strängar adderas (sammafogas).

===   Villkor som kontrollerar värdet OCH datatypen.

while-loopen: Villkoret kontrolleras före loopen.

for-loopen: Upprepar satser ett visst antal gånger.

Övningar

Övning 1

1. Skriv koden för felhantering överst i index.php från övning 2 i förra kapitlet (c:\xampp\htdocs\phpfiler\hej\index.php)
2. Prova att skriva något syntax-fel i echo-satsen
3. Testkör!


Övning 2

Syntax (Excercise 1 - 5)
Uppgift 1     Uppgift 2     Uppgift 3     Uppgift 4     Uppgift 5

Variables (Excercise 1 - 2)
Uppgift 1     Uppgift 2

Operators (Excercise 1 - 4)
Uppgift 1     Uppgift 2     Uppgift 3     Uppgift 4

If-Else (Excercise 1 - 4)
Uppgift 1     Uppgift 2     Uppgift 3     Uppgift 4

Loops (Excercise 1 - 3)
Uppgift 1     Uppgift 3


Inlämningsuppgift

login     logout    

Exit tickets

Tabell

Example på tabell Skriv ett PHP-skript som visar en tabell med uträkningen av x² för varje tal mellan 1 och 100. Använd PHP-kod och en loop för att skapa tabellen.

Fakta