View sourcecode

The following files exists in this folder. Click to view.

test.php

42 lines UTF-8 Unix (LF)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
// Laddar om sidan med $_GET['mess'] om sådant inte finns
if(!isset($_GET['mess'])){
  
header('Location: ?mess=Felmeddelande......');
  exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width= , initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Testar olika sätt att skriva ut.</title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mini.css/3.0.1/mini-default.min.css">
  <style>
  .error{ color: red;}
  </style>
</head>
<body>
<h1>Utskrifter av strängar och variabler</h1>
<p>Här visar jag olika sätt att skriva ut strängar som innehåller variabler, som du kan se så är det inte så tydligt vad som funkar, när och hur. <br>
 <a href="?mess=Felmeddelande......">Länken med GET-variabel....</a></p>
<?php
echo "<h2>Använda GET-variabeln direkt....</h2>";
// Använda $_GET-variabeln
echo "<p class='error'>".$_GET['mess']."</p>";
echo 
"<p class='error'>{$_GET['mess']}</p>";
// echo "<p class='error'>$_GET['mess']</p>"; // Detta sättet fungerar inte....
echo "<h2>Göra om GET-variabeln till \$mess</h2>";
// Använda $mess som variabel
$mess = isset($_GET['mess']) ? $_GET['mess'] : "";
echo 
"<p class='error'>".$mess."</p>";
echo 
"<p class='error'>{$mess}</p>";
echo 
"<p class='error'>${mess}</p>";
echo 
"<h2>Använda funktionen <a href='https://www.w3schools.com/php/func_string_sprintf.asp' target='_blank'>sprintf()</a></h2>";
// Alternativa sätt att skriva ut
echo sprintf("<p class='error'>%s</p>", $_GET['mess']);
echo 
sprintf("<p class='error'>%s</p>", $mess);
?>
</body>
</html>