What I learned in my PHP bootcamp

Image from Unsplash

Last year I did a coding bootcamp at BAVC. As a non-coder person, I’m planning to share my learnings here. Maybe it’ll be useful or inspiring for you who is also non-coder! This time I’m talking about PHP.

PHP originally stood for Personal Home Page, but it now stands for the recursive acronym PHP — Hypertext Preprocessor. PHP is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.

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 also 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 <?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>

A majority of sites on the web right now run on PHP. Due in large part to its popularity as the language of choice for back-end of content management systems — CMS, like WordPress, Drupal, and Joomla.


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.

Note: Function names are case-insensitive.

<?

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.

Note:

  • 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.

Note:

  • 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;


?>



My name is Thaisa, I’m a Project/ Product Manager from Brazil and I’m based in San Francisco, US.

More about me @ my Portfolio, LinkedIn, Instagram, Facebook, and Twitter. I’m also building an app if you want to get early access subscribe to the waiting list here!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.