Php Code – Codemeets – 03

php code 3. Today we going to learn more about . 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 code 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 have two method. it is 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 example array declaring. array counting start in 0. (out put also given below try it)

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

Array Using Key

You can Understand that code using example below.

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

This code out put is A+.

Functions in php code

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 function for the php. Using functions we can easily type small number of code and create a very good program.

you can get idea for using this code about functions. it simple steps

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

You can see output in this code Hello Tom and Hello Jack. you can try different way using this code.

Return Type

This is output of function. See 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 for all! practice well and try coding guys

More php ClickHere!

See you Next php Lesson Soon!

You may also like...

Leave a Reply

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

Translate »