Principles of PHP

Here is a very short introduction to the principles of PHP.  PHP is an acronym for “hypertext preprocessor”. PHP is a serverside language. We use PHP in order to prepare webpages, react to user input – and (on the third semester) to interact with databases.

Many Open Source CMSs use a combination of Apache, PHP and MySQL as an “engine”.

How to write PHP code

A php file is made in a manner similar to a HTML file. You create a file and give it the surname .php. So a file name sample could be:

  • myFile.php

The file can contain other forms of code and tags such as JavaScript, CSS or HTML. In the file the php code is nested between a <? and ends with a ?>.

Variables, Strings and numbers

<?php
$a = 10; // a number
$b = “Hello World”; // a string
$c = “Ho, ho, hooo!”; // yes it’s another string
$d = “Santa Claus, says: “; // xmas is near
$combinedStrings = $d . $c; // Santa Claus, says: Ho, ho, hooo!
$e = NULL; // no value set
$f = true; // or false
?>

A variable is defined via the $ character. In the sample code above you can see several variables.

A string starts and ends with a quotation mark as ” or ‘. If you need quotation marks inside a string they should be nested like this:

<? $foo = “<img src=’image.png’  alt=’an image’ />”; ?>

You can create HTML code via PHP – and you can show an image on a web page like this:

<? echo $foo; ?>

or

<? print(“$foo”); ?>

Numbers

Since a variable can be a number, you can use PHP for calculations, such as:

<?
$x = 1;
$y = 19;
echo $x + $y; // should return 20
?>

See this page for more information about PHP and calculations.

Conditions

Arrays

Loops

Includes

<?
include(“myFile.php”);
require(“myFile.php”);
require_once(“myFile.php”);
?>

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Enable Notifications OK No thanks

Vi anvender cookies for at forbedre din oplevelse, analysere trafik og tilpasse indhold. læs videre / more information

Multimusen.dk will set a few cookies from Doubleclick, Google and the Social Media plugins they ay set some cookies. Some of my pages use APIs - such as YouTube, LinkedIn, Google Fonts, Google Maps, Mapbox, Spotify, Jetpack, Twitter, Facebook &c.. Such plugins may set the odd cookie.

Close