Monday, May 9, 2016

WDP - PHP Date & Time

PHP Date and Time

The date() function formats a local date and time, and returns the formatted date string using the given integer timestamp or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time().

date(format,timestamp);

string date ( string $format [, int $timestamp = time() ] )
The required format parameter of the date() function specifies how to format the date (or time).
Here are some characters that are commonly used for dates:
  • d - Represents the day of the month (01 to 31)
  • m - Represents a month (01 to 12)
  • Y - Represents a year (in four digits)
  • l (lowercase 'L') - Represents the day of the week
Other characters, like"/", ".", or "-" can also be inserted between the characters to add additional formatting.
<?php// set the default timezone to use. Available since PHP 5.1date_default_timezone_set('UTC'. "<br>";

// Prints something like: Mondayecho date("l"
. "<br>";
// Prints something like: Monday 8th of August 2005 03:12:46 PMecho date('l jS \of F Y h:i:s A'
. "<br>";

// Prints: July 1, 2000 is on a Saturdayecho "July 1, 2000 is on a " date("l"mktime(000712000)) 
. "<br>";
/* use the constants in the format parameter */
// prints something like: Wed, 25 Sep 2013 15:28:57 -0700
echo date(DATE_RFC2822
. "<br>";
// prints something like: 2000-07-01T00:00:00+00:00echo date(DATE_ATOMmktime(000712000)) 
. "<br>";
?>
Here are some characters that are commonly used for times:
  • h - 12-hour format of an hour with leading zeros (01 to 12)
  • i - Minutes with leading zeros (00 to 59)
  • s - Seconds with leading zeros (00 to 59)
  • a - Lowercase Ante meridiem and Post meridiem (am or pm)
The example below outputs the current time in the specified format:

Example

<?php
echo "The time is " . date("h:i:sa");
?>
Output: The time is 09:54:12pm
Other formats...

Tuesday, May 3, 2016

WDP - Selection Structure and Php Loop

Php

  1. Selection Structure
    • if statement - executes some code if one condition is true

Syntax

if (condition) {
    code to be executed if condition is true
;
}

Example: 

<?php

if ($a > $b)

  echo "a is bigger than b";

?>

    • if... else statement - executes some code if a condition is true and another code if that condition is false

Syntax

if (condition) {
    code to be executed if condition is true;
} else {
    code to be executed if condition is false;}
Example

<?php
if ($a $b) {
  echo 
"a is greater than b";
} else {
  echo 
"a is NOT greater than b";
}
?>

    • if... elseif... else statement - executes different codes for more than two conditions

Syntax

if (condition) {
    code to be executed if this condition is true;} elseif (condition) {
    code to be executed if this condition is true;} else {
    code to be executed if all conditions are false;}
Example

if($a > $b){
    echo $a." is greater than ".$b;
}
elseif($a == $b){
echo $a." is equals to ".$b;
}
else{
echo $a." is neither greater than or equal to ".$b;
}
    • switch statement - selects one of many blocks of code to be executed

Syntax

switch (n) {
    case label1:
        code to be executed if n=label1;
        break;
    case label2:
        code to be executed if n=label2;
        break;
    case label3:
        code to be executed if n=label3;
        break;
    ...
    default:
        code to be executed if n is different from all labels;
}
Example

<?php
    $i = "bar";
switch ($i) {
    case "apple":
        echo "i is apple";
        break;
    case "bar":
        echo "i is bar";
break;
    case "cake":
        echo "i is cake";
        break;
}
}?>

  1. Php Loops
    • while statement - execute a block of code while the specified condition is true.

Syntax

while (condition is true) {
    code to be executed;
}
Example

<?php /* example 1 */
$i 1;
while (
$i <= 10) {
    echo 
$i++;  /* the printed value would be
                   $i before the increment
                   (post-increment) */
}
/* example 2 */
$i 1;
while (
$i <= 10):
    echo 
$i;
    
$i++;
endwhile;
?>

    • do... while statement - loops through a block of code once, and then repeats the loop as long as the specified condition is true

Syntax

do {
    code to be executed;} while (condition is true);
Example

<?php 
$x = 1; 

do {
    echo "The number is: $x <br>";
    $x++;
} while ($x <= 5);
?>

    • for statement -  loops through a block of code a specified number of times

Syntax

for (init counter; test counter; increment counter) {
    code to be executed;
}
Example

for ($i = 1; $i < 10 ; $i++) {
    echo $i;
}

    • foreach statement - loops through a block of code for each element in an array

Syntax

foreach ($array as $value) {
    code to be executed;
}
Example

<?php
foreach (array(2, 3, 4, 5) as $value) {

    $value = $value * 2;

echo $value."<br/>";

}
?>

Monday, May 2, 2016

WDP - PHP


PHP

  • Explain PHP?

PHP (recursive acronym for PHP: Hypertext Preprocessor), PHP is known as a server-sided language, is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.

  • Explain WAMP

Acronym for Windows/Apache/MySQL/PHP, Python, (and/or) PERL.
It is a package used by PHP developers to develop their web projects on their local windows system.
It comes with Apache server , MySQL database , PHPMyAdmin and PHP creating a suitable environment to development.

  • Explain the difference between procedural and object-oriented programming in PHP.

Procedural programming uses a list of instructions to tell the computer what to do step-by-step. Procedural programming relies on - you guessed it - procedures, also known as routines or subroutines. A procedure contains a series of computational steps to be carried out. Procedural programming is also referred to as imperative programming. Procedural programming languages are also known as top-down languages.
Procedural programming is intuitive in the sense that it is very similar to how you would expect a program to work. If you want a computer to do something, you should provide step-by-step instructions on how to do it. It is, therefore, no surprise that most of the early programming languages are all procedural. Examples of procedural languages include Fortran, COBOL and C, which have been around since the 1960s and 70s.

Object-oriented programming, or OOP, is an approach to problem-solving where all computations are carried out using objects. An object is a component of a program that knows how to perform certain actions and how to interact with other elements of the program. Objects are the basic units of object-oriented programming. A simple example of an object would be a person. Logically, you would expect a person to have a name. This would be considered a property of the person. You would also expect a person to be able to do something, such as walking. This would be considered a method of the person.
A method in object-oriented programming is like a procedure in procedural programming. The key difference here is that the method is part of an object. In object-oriented programming, you organize your code by creating objects, and then you can give those objects properties and you can make them do certain things.
A key aspect of object-oriented programming is the use of classes. A class is a blueprint of an object. You can think of a class as a concept, and the object as the embodiment of that concept. So let's say you want to use a person in your program. You want to be able to describe the person and have the person do something. A class called 'person' would provide a blueprint for what a person looks like and what a person can do. Examples of object-oriented languages include C#, Java, Perl and Python.

  • Identify the rules in naming variables in PHP.

A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).
Rules for PHP variables:
  • A variable starts with the $ sign, followed by the name of the variable
  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive ($age and $AGE are two different variables)


Reference:
  • http://www.homeandlearn.co.uk/php/php1p1.html
  • http://php.net/manual/en/intro-whatis.php
  • https://www.quora.com/What-is-WAMP-used-for
  • http://www.webopedia.com/TERM/W/WAMP.html
  • http://www.w3schools.com/php/php_variables.asp
  • http://study.com/academy/lesson/object-oriented-programming-vs-procedural-programming.html

CN - IP Subnetting - Class C Exercise

Computer Network

Exercise 1 : You are given an IP Address of 192.168.10.0/26. Calculate how many subnets and host per subnet are available in this network. Create the table for this network:

N =22 = 4 Network
H = 26 = 64 – 2 = 62 Host
Network Address
Valid Host Range
Broadcast Address
Subnet Mask
193.168.10.0
192.168.10.1 – 192.168.10.62
192.168.10.63
255.255.255.192
192.168.10.64
192.168.10.65 - 192.168.10.126
192.168.10.127
255.255.255.192
192.168.10.128
192.168.10.129 - 192.168.10.190
192.168.10.191
255.255.255.192
192.168.10.192
192.168.10.193 - 192.168.10.254
192.168.10.255
255.255.255.192


Exercise 2 : You are given an IP Address of 223.14.3.0/26. Calculate how many subnets and host per subnet are available in this network. Create the table for this network:

N =24 = 16 Network
H = 24 = 16 – 2 = 14 Host
Network Address
Valid Host Range
Broadcast Address
Subnet Mask
223.14.3.0
223.14.3.1 - 223.14.3.14
223.14.3.15
255.255.255.240
223.14.3.16
223.14.3.17 - 223.14.3.30
223.14.3.31
255.255.255.240
223.14.3.32
223.14.3.33 - 223.14.3.46
223.14.3.47
255.255.255.240
223.14.3.48
223.14.3.49 - 223.14.3.62
223.14.3.63
255.255.255.240
223.14.3.64
223.14.3.65 - 223.14.3.78
223.14.3.79
255.255.255.240
223.14.3.80
223.14.3.81 - 223.14.3.94
223.14.3.95
255.255.255.240
223.14.3.96
223.14.3.97 - 223.14.3.110
223.14.3.111
255.255.255.240
223.14.3.112
223.14.3.113 - 223.14.3.126
223.14.3.127
255.255.255.240
223.14.3.128
223.14.3.129 - 223.14.3.142
223.14.3.143
255.255.255.240
223.14.3.144
223.14.3.145 - 223.14.3.158
223.14.3.159
255.255.255.240
223.14.3.160
223.14.3.161 - 223.14.3.174
223.14.3.175
255.255.255.240
223.14.3.176
223.14.3.177 - 223.14.3.190
223.14.3.191
255.255.255.240
223.14.3.192
223.14.3.193 - 223.14.3.206
223.14.3.207
255.255.255.240
223.14.3.208
223.14.3.209 - 223.14.3.222
223.14.3.223
255.255.255.240
223.14.3.224
223.14.3.225 - 223.14.3.238
223.14.3.239
255.255.255.240
223.14.3.240
223.14.3.241 - 223.14.3.254
223.14.3.255
255.255.255.240

Further example and exercise here