Skip to content

Instantly share code, notes, and snippets.

@dkraczkowski
Last active February 9, 2024 20:24
Show Gist options
  • Save dkraczkowski/f1428ca9543235da69f9f4ec04f1ea8c to your computer and use it in GitHub Desktop.
Save dkraczkowski/f1428ca9543235da69f9f4ec04f1ea8c to your computer and use it in GitHub Desktop.
PHP the hard way.md

PHP the hard way!

About you

This course is for humans only. To be more particular you are a human with some computer science knowledge (programming especially)- you know terms like: variable, constant, function, data type, array and you are quite comfortable with using your computer.

About this course

What the hard way means? More or less that I am not going to put here useless knowledge and tons of descriptions. Idea of this course is to go example by example with no or really small explanation. Each example is going to be your teacher. Your role is to play with examples, understand how it works and mix them up to solve concrete problem which is going to be raised at the end of each section. Programming is about logical thinking and problem solving.

Preparing your environment

In order to start our journey we have to make sure php is installed and it is working on your machine. Your first problem to solve is to find out how to install php on your machine and make it running. If you thought I am going to show you how to do this, well I think you didn't get the hard way part.

Hint: https://github.com/rlerdorf/php7dev/  

You can also check up the followin link for running examples online: http://www.writephponline.com

Introduction to php

First program

<?php
Exercise

Run it in your enviroment

Comments

<?php
// This is an inline comment
# Another inline comment style - mostly not used.
/**
 * And this is multi-line comment. 
 * 
 * Comments are part of the language but they are totally ignored by parser.
 * You should use them to put some important thoughts for you/audience that are hard to express in code. 
 */

Declaring variable(s)

<?php
$number = 0;
$text = "Here goes text which can contain other variables, like this number: $number";
$plainText = 'Here goes only text';
$realNumber = 1.23;
$somethingThatIsTrueLikeILoveBeacon = true;
$somethingThatIsFalseLikeILovePeople = false;
$listOfValues = [0, 0.1, true];
$nothing = null;
Exercise

Run it in your enviroment and understand different data types that exists in PHP.

Working with numbers

<?php
$a = 2;
$b = 3;

$c = $a + b; // addition 5
$c = $b - $a; // substraction 1
$c = $a * $b; // multiplication 6
$c = $a / $b; // division 0.66666666666667
$c = $a % $b; // modulo 2
$c = $a ** $b; // exponentiation 8

$c = $a++; // incrementation 2 but $a is now 3
$c = ++$c; // incrementation 3
$c = $b--; // decrementation 3 but $b is now 2
$c = --$c; // decremenration 2
Exercise

Calculate how many seconds there is in an odd month and how many in an even, find out the difference between them.

Working with strings

<?php
$a = 'Hello';
$b = 'friend';

$c = $a[0]; //'H'
$c = $a[1]; //'e'
$c = $a[2]; //'l'
$c = $a[3]; //'l'
$c = $a[4]; //'o'
$c = $a[5]; //''

$c = $a . $b; //'Hellofriend'
$c = $a . ' ' . $b; //'Hello friend'
$c = "$a $b"; //'Hello friend'

Executing group of instructions known as function(s)

<?php
print('I didnt expect you to reach that far');
Exercise

Find all functions responsible for echoing output and play with them, String manipulating functions can be found here

Declaring function(s)

<?php
function printMessage()
{
    print('Message');
}

printMessage();

Parametrizing your function(s)

<?php
function printMessage($message)
{
    echo $message;
}

printMessage('Message');

Defining constant(s)

<?php
define('CONSTANT_NAME', 'this value will not change');
print(CONSTANT_NAME);
Array
<?php
$odd = [1, 3, 5];
$even = array(2, 4, 6);

print_r($odd[0]); // 1
print_r($even[2]); // 6
Hashes
<?php
$me = [
    'name' => 'PHP',
    'surname' => 'Hard'
];

$you = [
    'age' => 'unkown'
];

print_r($me['name']); //'PHP'
Working with arrays
<?php
$odd = [1, 3, 5];
$even = [2, 4, 6];

$all = array_merge($odd, $even);
print_r($all); // 1, 3, 5, 2, 4, 6
sort($all);
print_r($all); // 1, 2, 3, 4, 5, 6
unset($all[5]);
print_r($all); // 1, 2, 3, 4, 5
@Ioannis-K
Copy link

nice!
I strongly believe that this is the best way of learning computer languages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment