Outputs

Introduction into PHP

On the following pages you find an introduction into the basics of PHP. At the end there are also more advanced topics, like using an IDE. Have fun with the workshop and with learning PHP!

PHP-Tags

As you could see in the firsts script, php uses special HTML-Tags to seperate the PHP-Code from the HTML-Code. In general PHP allows multiple tags, but the best practice is to use the following tag for the beginning of PHP-Code:

<?php 

And to close a PHP-block you use:

 

?>

With these tags the PHP-parser knows, that a PHP code part is starting and can parse and execute it.

PHP Output with echo and print

There are multiple possibilities to do an output with PHP:

echo

Echo kann be used to output some text, it is not a function. Therefore you do not need to use quotes, but you can use them.

The following example shows some possibilities how ech can be used to do different outputs.

<?php
   $vorname = "Heinz";
   $nachname = "Müller";

   //Diese Ausgabe gibt "Heinz" aus
   echo $vorname;

   //Diese Ausgabe gibt "Müller" aus
   echo $nachname;

   //Diese Ausgabe gibt HeinzMüller aus
   echo $vorname, $nachname;

   //Die Ausgabe gibt "Mein Name ist Heinz Müller" aus
   echo "Mein Name ist $vorname $nachname";

   //Diese Ausgabe gibt "Mein Name ist $vorname $nachname" aus
   echo 'Mein Name ist $vorname $nachname';
?>

print

print is also not a real PHP-function, you can use print as a function (with rounded breakets).

The difference between print and echo is:

echo is an internal language construct and does not return any value, therefore you can not use echo in combination with an expression. Beside that you can use echo with multpile arguments (seperated by ","), print does not allow that.

Note: in practive you would most likely use a template engine like, twig, fluid or smarty and you don't to the outputs by your own.

Tipps for a good PHP-code style

You've learned about how to do outputs with PHP. Theoretically you could do everything with a PHP-Block and yous output all HTML-Tags with echo.

But does that make sence?

No! It is hard to understand the code later. We propose the avoid the output of HTML with PHP (like echo "<p>test</p>)".

Beside that it is good to add comments to your code blocks, loops and functions.

Comments in PHP-Code

Comments can help you to make notes in the code and to explain something. These comments are irgnored by the PHP-parser and also not executed.

They are only for the developers. Partly these comments can also be used to generate a api documentation from the source code.

It is good to use comments frequently! It can help you and others to understand the code now and in weeks. You can not imagine how fast you forget about that code and do not know what is done in that part. These comments can also help new developers in a project to get used to the code.

There are multiple types of comments in PHP:

One line comments:

<?php
   //Einfache Ausgabe
   echo "Hallo";
?>

Multi line comments:

<?php
   /*
   * Wir geben mit der Funktion
   * echo einen Text aus.
   */
   echo "Hallo";
?>

multi line docblock comments:

<?php
   /**
   * @author Timo Schmidt
   */
?>

The multiline docblock comments can be used to generate an outmated api documentation from the source code. The name of that standard is java doc standard. We will have a deeper look into them later. But before we can use functions and methods in PHP we need to learn more PHP-basics.

Navigation