You are currently viewing Day 2: Basic Keywords in PHP – Part 2

Day 2: Basic Keywords in PHP – Part 2

  • Post author:
  • Post category:PHP

[et_pb_section transparent_background=”off” allow_player_pause=”off” inner_shadow=”off” parallax=”off” parallax_method=”on” padding_mobile=”off” make_fullwidth=”off” use_custom_width=”off” width_unit=”off” custom_width_px=”1080px” custom_width_percent=”80%” make_equal=”off” use_custom_gutter=”off” fullwidth=”off” specialty=”off” admin_label=”section” disabled=”off”][et_pb_row make_fullwidth=”off” use_custom_width=”off” width_unit=”off” custom_width_px=”1080px” custom_width_percent=”80%” use_custom_gutter=”off” gutter_width=”2″ padding_mobile=”off” allow_player_pause=”off” parallax=”off” parallax_method=”on” make_equal=”off” column_padding_mobile=”on” parallax_1=”off” parallax_method_1=”on” parallax_2=”off” parallax_method_2=”on” parallax_3=”off” parallax_method_3=”on” parallax_4=”off” parallax_method_4=”on” admin_label=”row” disabled=”off”][et_pb_column type=”4_4″ disabled=”off” parallax=”off” parallax_method=”on” column_padding_mobile=”on”][et_pb_text background_layout=”light” text_orientation=”left” admin_label=”Text” use_border_color=”off” border_style=”solid” disabled=”off”]

Ok, I appreciate those that comment on our previous topic. it tells me some people are interested. I will advise you to follow the tutorial and practice it on your system.

“Practice makes Perfect”

No biddle-diddle, straight to the point. Let’s talk about the basic keywords we mentioned earlier. See: Day 1: Introduction to PHP/Mysql.

Assuming you have installed xampp and Dreamweaver. On windows, go to “C:xampphtdocs”. if you install it on another drive, check the drive, you will see xampp, open it you’ll see htdocs, open htdocs. Htdocs is like your hosting space online (i.e. public_html) where you keep your website files.

Example 1:We want to create a website to perform simple calculation using php.

Create a folder inside htdocs, call it calculator. Open Dreamweaver, go to File > New (Ctrl + N), Select Dynamic Page, Under dynamic page choose PHP. New PHP page is created. Save it as index.php into calculator folder.

In Dreamweaver, at the top left part, you will see three buttons they are; CODE | SPLIT | DESIGN

Dreamweaver is a nice software. You can click DESIGN to design your HTML/CSS website without coding. Automatically Dreamweaver will create the code for you. To see the code, click CODE. SPLIT lets you see the CODE and the DESIGN view splitting the window into two for you.

Hope, you understand how dreamweavercode | split | design button works. Now to start writing our php code, click CODE button. You will see the following HTML code;
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” ““>
<html xmlns=”“>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Untitled Document</title>
You can change your title to something like Simple Calculator; like this  <title>Simple Calculator</title>

Write your code and other site content within the body tag i.e. <body> </body>

<body> </body>.
Ok, let’s solve our problem. What’s it again? “creating a website to perform a simple calculation using PHP“.

Now, this is where your creativity comes in. You need to think on how to achieve this. Different people can do this in
different ways and still achieve the same result.

#1 We need a form. One input box to accept first value from user, second box to accept second value from the same user, and a button to click. See the code below;
<form name=”frmname” action=”” method=”post”>
<input type=”number” required name=”num1″ placeholder=”Enter Your First Number” />
<input type=”number” required name=”num2″ placeholder=”Enter Your Second Number” />
<button name=”cmdsubmit”>Calculate</button>
Now, Place the code before form tag;
echo ‘<h2>Welcome to Simple Calculator</h2>’;
if($_POST[“num1″] == ”){ echo ‘First number is required’; }
elseif($_POST[“num2″] == ”){ echo ‘Second number is required’; }
$result = $_POST[“num1”] + $_POST[“num2”];
echo ‘Result is ‘.$result;
Assuming you know HTML already, so no need to explain the form tags above. Let’s explain the PHP code above.

First line:  echo ‘<h2>Welcome to Simple Calculator</h2>’; Print out the content in single quote ‘’ on webpage. to print out simple on website from php, use echo follow by string.

Second line: this is if statement
if (condition){…..}
if cmdsubmit which is submit button is submitted perform the next state line. So, we use isset() which is another php keyword to check if form is submitted; isset($_POST[“cmdsubmit”])

Next statement line;
if($_POST[“num1″] == ”){ echo ‘First number is required’; }
elseif($_POST[“num2″] == ”){ echo ‘Second number is required’; }
We validated the form, checking if num1 is not empty, and num2 is not empty. if they are empty echo. Else, i.e. if num1 and num2 is not empty, perform the code;
$result = $_POST[“num1”] + $_POST[“num2”];
echo ‘Result is ‘.$result;
The code above calculate the two numbers and store the value in $result. $result is a variable. In php, this is how to declare a variable. You can use any name but be careful, don’t use keyword as your variable. You can use something like this;
$r, or $rs, $result, $result_calc

Do not declare a variable like this; $result-calc. It totally wrong. If you do this the system will throw error.
And don’t forget, at the end of each statement line is a semi-colon (;) If you omit this also the system will throw error.

Later, we will talk about the declaration of Variable and Constant, PHP Syntax, Type of Error