Skip to content

Latest commit

 

History

History
488 lines (448 loc) · 12.2 KB

php-cert-aug-2022.md

File metadata and controls

488 lines (448 loc) · 12.2 KB

PHP Certification - Aug 2022

Homework

For Mon 22 Aug 2022

  • Quiz Questions for Topic 7 (OOP)
  • Mock Exam #1
  • Mock Exam #2

For Weds 10 Aug 2022

  • Quiz questions for Topic 1 (Syntax/Basics) For Fri 19 Aug 2022
  • Quiz questions for Topic 2 (Data Formats and Types)
  • Quiz questions for Topic 3 (Strings and Patterns)
  • Quiz questions for Topic 4 (Arrays)
  • Quiz questions for Topic 5 (I/O)
  • Quiz questions for Topic 6 (Functions)
  • Mock Exam #1

TODO

Docker Container Setup

  • Download the ZIP file from the URL given by the instructor
  • Unzip into a new folder /path/to/zip
  • Follow the setup instructions in /path/to/zip/README.md

Class Notes

Basic representation of integers

<?php
namespace abc {
	define('WHATEVER', 'Whatever', TRUE);
	const ANYTHING = 'Anything';
}

namespace xyz {
	echo WHATEVER;
	echo ANYTHING;
}

Bitwise Operators

Tutorial oriented towards the exam:

<?php
echo "Logical AND\n";
printf("%04b\n", 0b00 & 0b00);
printf("%04b\n", 0b00 & 0b01);
printf("%04b\n", 0b01 & 0b00);
printf("%04b\n", 0b01 & 0b01);

echo "Logical OR\n";
printf("%04b\n", 0b00 | 0b00);
printf("%04b\n", 0b00 | 0b01);
printf("%04b\n", 0b01 | 0b00);
printf("%04b\n", 0b01 | 0b01);

echo "Logical XOR\n";
printf("%04b\n", 0b00 ^ 0b00);
printf("%04b\n", 0b00 ^ 0b01);
printf("%04b\n", 0b01 ^ 0b00);
printf("%04b\n", 0b01 ^ 0b01);

Examples of the three ops:

<?php
$a = 0b11111111;
$b = 0b11011101;

printf("%08b", $a & $b); // 1101 1101
printf("%08b", $a | $b); // 1111 1111
printf("%08b", $a ^ $b); // 0010 0010

Left/right shift illustration:

<?php
echo 16 << 3;
echo "\n";
echo 0b10000000;
echo "\n";

echo 16 >> 3;
echo "\n";
echo 0b00000010;
echo "\n";

echo 15 >> 3;
echo "\n";
echo 0b00000001;
echo "\n";

Nested Ternary Construct

$a = 30;
$b = 20;
echo ($a < $b) ? 'Less' : (($a == $b) ? 'Equal' : 'Greater');
// output: "Greater"

Null coalesce operator example

$token = $_GET['token'] ?? $_POST['token'] ?? $_COOKIE['token'] ?? 'DEFAULT';

php.ini file settings:

Caching

See: https://www.zend.com/blog/exploring-new-php-jit-compiler

  • Look for the "Before JIT" section

Garbage Collection

Data Formats

Read up on SimpleXMLElement

<?php
// for relative formats see:
// https://www.php.net/manual/en/datetime.formats.relative.php
$date[] = new DateTime('third thursday of next month');
$date[] = new DateTime('now', new DateTimeZone('CET'));
$date[] = new DateTime('@' . time());
$date[] = (new DateTime())->add(new DateInterval('P3D'));
var_dump($date);
  • SimpleXML example
<?php
$xml = <<<EOT
<outer>
	<A>
		<B>B11</B>
		<B>B12</B>
		<B>B13</B>
	</A>
	<A>
		<B>B21</B>
		<B>
			<C>C1</C>
			<C>C2</C>
			<C>C3</C>
		</B>
		<B>B23</B>
	</A>
</outer>
EOT;
$obj = new SimpleXMLElement($xml);
var_dump($obj);
  • JSON example
<?php
$json = <<<EOT
{
	"name":"Fred Flintstone",
	"age" : 39,
	"address": {
		"city" : "Bedrock",
		"state" : "Whatever",
	},
	"status": 999
}
EOT;
try {
	$data = json_decode($json);
	var_dump($data);
} catch (Throwable $t) {
	echo $t;
}
if (json_last_error() !== JSON_ERROR_NONE) {
	echo json_last_error();
	echo "\n";
	echo json_last_error_msg();
	echo "\n";
}

Strings

  • stripos() example of potential problem when used with if
<?php
$str = 'The quick brown fox jumped over the fence.';
echo (stripos($str, 'the')) ? '"the" was found' : '"the" was NOT found';
echo " in $str\n"; // returns "NOT found"
echo $str[0] . "\n";

echo (stripos($str, 'the') !== FALSE) ? '"the" was found' : '"the" was NOT found';
echo " in $str\n";	// returns "found"
  • Study the docs on sprintf() to get format codes for that family of functions
  • Example using negative offsets:
<?php
$dir = '/home/doug/some/directory/';
if (substr($dir, 0, 1) === '/') echo 'Leading slash' . PHP_EOL;
if (substr($dir, -1) === '/') echo 'Trailing slash' . PHP_EOL;
if ($dir[-1] === '/') echo 'Trailing slash' . PHP_EOL;
<?php
$text = 'Doug Bierer';
$patt = '/(.*)\s(.*)/';
echo preg_replace($patt, '$2, $1', $text);
  • preg_replace() and preg_match() example using sub-patterns:
<?php
$string = 'April 15, 2003';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = '$2 $1 $3';
echo preg_replace($pattern, $replacement, $string);

preg_match($pattern, $string, $matches);
var_dump($matches);

Arrays

For iterating through an array beginning-to-end don't forget about these functions:

  • array_walk()
  • array_walk_recursive()
  • array_map() Also: please don't forget the array navigation functions:
  • reset(): sets pointer to top
  • end() : sets pointer to end
  • prev() : advances array pointer
  • next() : un-advances array pointer
  • key() : returns index value at array pointer
  • current() : returns value of element at array pointer

I/O

Streams

  • Don't have to study all functions, just certain of the more common ones
  • https://www.php.net/streams
    • stream_context_create()
    • stream_wrapper_register()
    • stream_filter_register()
    • stream_filter_append()
    • stream_socket_client() In addition to the informational file functions mentioned, you also have:
  • fileatime()
  • filemtime()
  • filectime() etc. Wrappers
  • https://www.php.net/manual/en/wrappers.php

Functions

<?php
function superDump()
{
	var_dump(func_get_args());
}

$a = new ArrayIterator([1,2,3]);
$b = [4,5,6];
$c = 'Test';

superDump($a, $b, $c);

OOP

<?php
class Test
{
	public $a = 0;
	public $b = 0;
    public $c = 'Test';
    public $d = [];
    public $e = '';
	public function __construct(int $a, float $b, string $c, array $d)
	{
		$this->a = $a;
		$this->b = $b;
		$this->c = $c;
		$this->d = $d;
		$this->e = md5(rand(1111,9999));
	}
	public function __sleep()
	{
		return ['a','b','c','d'];
	}
	public function __wakeup()
	{
		$this->e = md5(rand(1111,9999));
	}
}
$test = new Test(222, 3.456, 'TEST', [1,2,3]);
var_dump($test);
$str = serialize($test);
echo $str . PHP_EOL;

$obj = unserialize($str);
var_dump($obj);

Another example of serialize() and unserialize()

<?php
class Test
{
	public $name = '';
	public $address = ['city' => 'Surin', 'country' => 'TH'];
	public $member = TRUE;
	public $id = 101;
	public $amount = 99.99;
	public $time = NULL;
	public $sensitive = '';
	public function __construct()
	{
		$this->time = new DateTime('now');
		$this->sensitive = md5(rand(100001,999999));
	}
	public function __toString()
	{
		return serialize($this);
	}
	public function __sleep()
	{
		return ['name','address','member','id','amount'];
	}
	public function __wakeup()
	{
		self::__construct();
	}
}
$test = new Test();
$str  = serialize($test);
echo $test . PHP_EOL;
echo $str . PHP_EOL;

$obj = unserialize($str);
echo ($test == $obj) ? 'SAME' : 'NOT SAME';
echo PHP_EOL;
echo $obj . PHP_EOL;

var_dump($test);
var_dump($obj);

Database Topic

Fetch Modes:

Security Topic

Questions are drawn from here:

Error Handling

Example of aggregated Catch block:

try {
    $pdo = new PDO($params);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException | Exception $e) {
    error_log('Database error: ' . date('Y-m-d H:i:s'));
} catch (Throwable $e) {
    error_log('Any and all errors or exceptions: ' . date('Y-m-d H:i:s'));
} finally {
    echo 'Database connection ';
    echo ($pdo) ? 'succeeded' : 'failed';
}

Change Request

// Module // | Slide w/in the module // | | Key: "http://localhost:4444/#/2/82"

Mock Exam 2

Question 3 is wrong. Number s/be "999.000.000,00"

Change Request for Code Repo

php_cert Repo for Class Demo