PM101
Published in

PM101

What I learned in My PHP Bootcamp

Photo by Alexandru Acea on Unsplash
The PHP mascot is the elePHPant, isn’t cool? It’s a blue — weird — elephant.

What does it mean?

PHP is a language designed primarily for web development but can also be used as a general-purpose programming language. What distinguishes PHP from something like JavaScript is that the code is executed on the server. PHP is enclosed in special start and end processing instructions (for opening,<?php and for closing ?>). It allows you to jump into and out of PHP mode.

<!DOCTYPE html>
<html>
<head>
<title>PHP is cool</title>
</head>
<body>
<?php echo '<p>Hello Medium</p>'; ?>
</body>
</html>

Functions

A function is a piece of code which takes one more input in the form of parameter and does some processing and returns a value.

<?

function sayHello()
{
echo "Hello!";
echo " and welcome to my Blog!" ;
}


echo "Today is Sunday August 28th, 2017. <br />";
sayHello();
sayHello();
sayHello();
sayHello();

?>

Array

An array is a special variable which can hold more than one value at a time. An ordered map that is a type that associates values to keys. In PHP, there are three types of arrays:

  • Indexed arrays — Arrays with numeric index
  • Associative arrays — Arrays with named keys
  • Multidimensional arrays — Arrays containing one or more arrays
<?
/* $name1 = "Madonna";
$name2 = "Kathleen Hanna";
$name3 = "Edith Piaf";
$name4 = "Nina Simone";
$name5 = "Madeleine Peyroux";
*/

$artists = array( "Madonna", "Kathleen Hanna", "Edith Piaf", "Nina Simone", "Madeleine Peyroux" );

// echo $artists[ 3 ] . "is the singer for Le Tigre";

for( $i = 0; $i < sizeof($artists); $i++ )
{
echo "Current Artist: " . $artists[ $i ];
echo "<br />";
}


?>

If else

The “if else” statement executes some code if a condition is true and another code if that condition is false.

<?
$myAge = 65;

if( $myAge >= 21 )
{
echo "Welcome to the show! <br />";

if( $myAge >=60 )
{
echo "You get a senior discount ticket!";
}
}
else if( $myAge >= 13 )
{
echo "You can come in but must stay in the teen area.";
}
else
{
echo "Sorry, you are too young.<br />";
echo "Come back in a few years.";
}

?>

Process form

Process form is the possibility to respond to user queries or data submitted from HTML forms. You can process information gathered by an HTML form and use PHP code to make decisions based off this information to create dynamic web pages.

<?

echo "Hello, " . $_GET[ "firstName"];
echo "<br>";
echo "The age entered was " . $_GET[ "age"];
echo "<br>";
echo "Favorite color is " . $_GET[ "favoriteColor"];

?>

Switch-case

The switch-case statement tests a variable against a series of values until it finds a match and then executes the block of code corresponding to that match.

<?

$name = "pete";

$name = strtolower( $name);

switch( $name )
{
case "paul": echo "Paul plays bass";
break;
case "john": echo "John plays guitar";
break;
case "george": echo "George plays electric guitar";
break;
case "ringo":
case "pete": echo $name . " plays drums";
break;

default: echo "That is not a Beatle";
break;
}

Variables

A variable starts with the $ sign, followed by the name of the variable. Variables are used to store data. Variable values can change over the course of a script.

  • A variable does not need to be declared before adding a value to it
  • 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 _ )
<? 

// this is a comment
/* this is also a comment */

$myName = "Thai";
echo "Hello, " . $myName;

$favorite_color = "purple";
echo "Your favorite color is " . $favorite_color;

$favoriteColor2 = "pink";
echo "Sometimes you also like " . $favoriteColor2;

?>

While Loop

The while loop statement tells PHP to execute the nested statement(s) repeatedly, as long as the while expression evaluates to TRUE.

<?
$age = 10;

while( $age <20 )
{
echo "Hello!<br>";
$age++;
}

?>

Signup Form

It’s a document that containing empty fields, that the user can fill the data or user can select the data.

<!DOCTYPE html>
<html>
<head>
<title>signup form</title>
</head>

<form name= "frmSignup" method="get" action="processForm.php">

Name: <input type="text" name="firstName" />
<br>
Age: <input type="text" name="age" />
<br>
Favorite Color: <input type="text" name="favoriteColor" />
<br>
<input type="submit" name="btnSubmit" value="Process Form" />

</form>

</body>
</html>

For Loops

For loops statements are the most complex loops in PHP. They behave like their C counterparts.

<?

for( $i = 0; $i < 5; $i++ )
{
echo "Hello! <br />";

for( $j = 0; $j < 3; $j++ )
{
echo "---Goodbye!<br />";

for( $k = 1; $k < 7; $k++ )
{
echo "Have a nice day! <br />";
}
}
}

?>

Increment

The PHP increment operators are used to increment a variable’s value.

  • They only affect numbers and strings
  • Arrays, objects and resources are not affected
<?

$years = 15;

// $years++;
// echo "Adam Levine has been singing for " . $years . " years";

echo "Adam Levine has been singing for " . $years-- . " years";

echo "<br>Adam has NOW been singing for " . $years . "years";

$myAge = 12;

// $myAge = $myAge + 6;
$myAge += 6;

echo "<br />my age is " . $myAge;


?>

👋 Feel Free to Clap and Share your Thoughts!

Find out more at our LinkedIn, Instagram and Twitter. If you’re looking for PM goodies, check out the Online store.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Thaisa Fernandes

Problem solver and perfectionist in recovery willing to stretch myself and risk making mistakes to achieve innovative solutions and validate my learnings