Php Course – Worldwideasy – 03

Php lesson 3. Today we going to learn more about it. Today I will focus on Getting User Input, Declaring, Arrays, Functions, and Return Type. Finally, Create a simple Calculator. It can provide extensive knowledge.

Getting User Input in php

Uses any computer language user input is how php does it. But it happens here in a very different way. We are focusing on that step by step.

To get the input you first need to create a form using HTML. The code below explains it to you.

<!DOCTYPE html>
<html>
<head>
  <title></title>
</head>
<body>

  <form action="site.php" method="get">
  Name:<input type="text" name="name">
  <input type="submit" name="submit"> 
   </form>

</body>
</html>

Next, we need to create a php file. When you click submit as mentioned above, it will link to the file site.php. Now let’s create that file. Below is the code.

<?php
echo $_GET["name"];
?>

Declaring

It has two methods. it is the get method and post method.

  • Post method is not show Details in URL.
  • Get method is Show Details in URL.
  • Post method have good Security.
<?php
$color = $_GET["color"];
?>

Arrays

An array is a large group or number of things. Numbers, symbols, etc. arranged in rows and columns Below is the code.

now you what is an array. now we doing how to apply array in php. you can see below the example array declaring. array counting starts at 0. (output also given below)

<?php
$friends = array ("kevin","lance","tom","apple","charls");
echo "friends[1]";                 // lance
echo count ($friends);             //5
?>

Array Using Key

You can Understand that using the example below.

<?php
$grade = array ("sanka"=>"A+","milinda"=>"B-","kasun"=>"C");
echo $grade["sanka"];
?>

This code output is A+.

Functions in php

A function is an organized, reusable code that performs a single and related function. Tasks reuse better modularity and higher code for your application.

Now we apply the function for the php. Using functions we can easily type a small number of code and create a very good program.

you can get an idea for using this code about functions.

<?php
function sayhi ($name){
echo "Hello $name";
}
sayhi("Tom");  //Hello Tom
sayhi("Jack"); //Hello Jack
?>

You can see the output in this code, Hello Tom and Hello Jack.

Return Type

This is the output of the function. See the below Example.

<?php
function num ($num){
return $num * $num * $num;
}
$result = num(4);  // 64
echo "result";
?>

this code answer is 64.

Calculator Example

<!DOCTYPE html>
<html>
<head>
  <title>Calculator</title>
</head>
<body>
  <center>
    <h1>Calculator</h1>
    <form action="site.php" method="get">
      <input type="number" name="num1">
      <br><br>
      <input type="number" name="num2">
      <br><br>
      <input type="submit">
    </form>
  </center>

  <?php
  echo $_GET["num1"]+$_GET["num2"];
  ?>

</body>
</html>
Output Calculator

Thank you for all!

More php ClickHere!

See you Next Lesson Soon!

Leave a Comment

Your email address will not be published. Required fields are marked *

Translate »