Boinx's Blog

Stepping stone

PHP: Hypertext Processor

I recently broadened my knowledge about PHP. I took the course PHP at codeacademy and I’m here to share some of the things I’ve learned.

PHP codes should be under this tag:

<?php
    you input your PHP code here....
?>

Declaring Variables

$num1 = 1;
$num2 = 2;
$num3 = 3;
$true = true;
$name = "Boinx";

In declaring variables, we must not forget the ‘$’ or else our variable will be invalid and we’ll receive an error message.

Using the variables we’ve declared

echo $num1;
echo $num2;
echo $num3;
echo $name;

In calling the variables we’ve declared we should not also forget the ‘$’.

Arrays

Standard Array

$myArray = array('2013','Hyperdunk','BHM');
echo "I have a". " " . $myArray[0] . " " . $myArray[1] . " " . $myArray[2];

This is how you make an array in PHP and how you use the values inside the array. The output of this is “I have a 2013 Hyperdunk BHM”

Associative Array

$myArray = array('year' => 2013,'model' => 'Hyperdunk','colorway' => 'BHM');

echo "I have a" . " " . myArray['year'] . " " . myArray['model'] . " " . myArray['colorway'];

or

$myArray = array('year' => 2013, 
                'model' => 'Hyperdunk',
                 'colorway' => 'BHM');

echo "I have a" . " " . myArray['year'] . " " . myArray['model'] . " " . myArray['colorway'];

You can write the code however you want it to be written as long as you follow the format (‘name’ => ‘value’).

Associative Arrays are more specific because you give names on the values of your arrays. Therefore it will be a lot easier to locate what index you’ll need to look for when you need a certain value.

The output of this is as same as the output of the Standard Array. “I have a 2013 Hyperdunk BHM.”

Objects

Class Friend{
    public $isAlive = true;
}

$friend1 = new Friend();
$friend1=>isAlive();

This is how we declare objects in PHP. $isAlive is a property of the Object friend, it is set to ‘public’ so that you can access it anywhere in the program. To create an instance of your Object:”$friend1 = new Friend();”. $friend1=>isAlive(); we are checking if our instance contains a propery name isAlive. As you can see we use “=>” instead of “.” because PHP does not use dot notation instead it uses the arrow notation.

Premade Functions of PHP

//String Functions
$myName = "Boinx";
strlen($myName);
substr($myName,0,4);
strtoupper($myName);
strtolower($myName);
strpos($myName,"o");

//Math Functions
round(MATH_PI);
round(MATH_PI,4);
floor(MATH_PI);
ceil(MATH_PI);

//Array Functions
$myArray = array();
array_push($myArray,'Boinx');
array_push($myArray,'LeBron');
array_push($myArray,"Durant");
count($myArray);
sort($myArray);
rsort($myArray);

String Functions
strlen() is a function that returns the length of a string. strlen($myName) returns 5.

substr() is a function that returns the value of the index of the string given. The arguments are: substr(string,index where it will start, index where will it end). So the value of substr($myName,0,4) is Boin.

strtoupper() is a function that converts the string to uppercase. strtoupper(string) is the correct format. The value of strtoupper($myName) is BOINX.

strtolower() is a function that converts the string to lowercase. strtolower(string) is the correct format. The value of strtolower($myName) is boinx.

strpos is a function that returns the index of a letter that is on a string. strpos(string,character to find) is the correct format. strpos($myName,”o”) returns 1;

Math Functions
(M_PI) is equals to 3.1416.

round() is a Math function that rounds off a decimal number. round(floatingnumber,number of decimal places) is the correct format. round(M_PI) returns 3 while round(M_PI,4) returns 3.1416.

floor() is a Math function that forcefully rounds down the decimal number. floor(M_PI) returns 3.

ceil() is a Math function that forcefully rounds up the decimal number. ceil(M_PI) returns 4.

Array Functions
array_push is a function that pushes a value to an array. array_push(arrayName,valueTobePushed) is the correct format. array_push($myArray,’Boinx’) pushes ‘Boinx’ to the array so the array now have its first value.

count() is a function that counts the length of an array. count(arrayName) is the correct format. count($myArray) returns 3.

sort() is a function that alphabetically sorts the values inside the array. sort(arrayName) is the correct format. sort($myArray) sorts the array to [“Boinx”,”Durant”,”LeBron”].

rsort() is a function that sorts the values inside the array in descending order. rsort(arrayName) is the correct format. rsort($myArray) sorts the array to [“LeBron”,”Durant”,”Boinx”].

Defining our Own Functions

function HelloWorld(){
    echo "Hello World";
}

HelloWorld();



//with argument

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

hello('Boinx');

This is how we define our own function in PHP. The format is:


function functionName(argument){

    something to do.........
}

HelloWorld() is how we call our function that doesn’t require you to pass an argument. hello(‘Boinx’) is an example on how to call your function when it requires you to send an argument.

That’s all for today! I’ll update this blog again as soon as I learned something new. Thanks for reading! :)