Skip to content
Marie-Louise edited this page Sep 17, 2018 · 46 revisions

PHP: Hypertext Preprocessor

How PHP works

when a user visits a dynamic site that built using PHP, the page sends a request to the server and then the server sends a request to the PHP Zend engine for processing. If there is a database the the Zend engine will send a request to it and this will process the results

PHP is an embedded language, so the code is embedded / mixed in HTML markup. PHP is a server side technology. after its been processed or parsed - it outputs only HTML and text.

Common features in PHP scripts

  • Variables act as placeholders for unknown or changing values. Variables documentation
  • Arrays hold multiple values
  • Conditional statements are used to make decisions
  • Loops are used to perform repetitive tasks

PHP files must be stored inside the server root folder (the names can vary):

  • htdocs

  • www

  • wwwroot

  • public_html

Basics

These two statements are used to display values in the browser. The main difference between them is that:

Echo can display a series of values separated by commas.

echo $number, ' ', $firstName;

Print can display only a single values

print $number;

Escape sequence docs

$book = 'Hitchhiker\'s guide to the galaxy';
echo $book;

Displaying a value in a string (must use double quotation marks)

echo "I love $book";

Concatenation

In programming this means to link in a chain or series. In PHP the concatenation operator is a . (dot). In the example below, I've added a literal space to separate the names.

$fullName = $firstName . '' . $lastName;
echo $fullName

another way to create a space is :

echo $fullName . '<br>';

combined concatenation operator is .=

Indexed array

arrays are similar to a list, but with values stored inside. You can't automatically convert an array to string in PHP. For that reason, the following lines of code do not work.

$characters = array ('Arthur Dent', 'Ford Prefect','Zaphor Beeblebrox');
echo $characters;

To inspect the elements inside the array, use:

print_r($characters);

When you view the contents of an array, each element has an index number starting from 0. This number is known as an array index.

Another way to write an array, similar to JavaScript, is using the square brackets:

$characters = ['Arthur Dent', 'Ford Prefect','Zaphor Beeblebrox'];

To add more values to the array, add an empty pair of square brackets after the variable name and assigning the value directly.

$characters[] = 'Marvin';
$characters[] = 'Slartibartfast';

These two new elements have been added to the previous array.

To access individual array elements:

echo $characters[1];

PHP functions


 <?php
 function hello(){
 echo 'Hello, World !';
 }
 hello();
 ?>

function arguments

information may be passed to functions through an argument list. The argument list is separated by commas ( comma delimited). The expressions are evaluated from left to right. Place the argument into a function by placing them into parentheses after the function name definition.

There are two ways

  1. passing by value - This is the default in php. The arguments value is not affected at all by the function outside of that function scope

  2. passing by reference - the value of an argument, if modified on the outside of that function scope.

    function hello($name){ echo "Hello, $name!"; } hello();


FILE : footer.tpl.php /sites/all/modules/custom/corp_markup/theme/sitewide/footer.tpl.php/footer.tpl.php

 <ul class="nav-social__list">
      <?php foreach($variables['social'] as $item): ?>
        <li class="nav-social__list__item">
          <a href="<?php print $item['url']; ?>"
             title="Follow us on <?php print $item['title']; ?>"
             class="nav-social__list__item__link nav-social__icon nav-social__icon--<?php print 
$item['class_modifier']; ?>"><?php
                 print "Wellcome on " . $item['title'];
          ?></a>
        </li>
      <?php endforeach; ?>
    </ul>

In order to render the desired labels to the social media icons, it was necessary to add the following code (the full code block is above) :

 print "Wellcome on " . $item['title'];

OTHER FILES :

/sites/all/modules/custom/corp_markup/theme/misc/social_media_share.tpl.php/social_media_share.tpl.php /sites/all/modules/custom/corp_markup/theme/sitewide/footer.tpl.php/footer.tpl.config.js /sites/all/modules/custom/corp_markup/corp_markup.module /sites/all/modules/custom/corp_sitewide/plugins/content_types/footer.inc

Clone this wiki locally