Php Code – Codemeets – 03
PHP Code Lesson 3: Getting User Input, Arrays, Functions, and a Simple Calculator
In this lesson, we will learn how to get user input, use arrays, create functions, and understand return types in PHP. Finally, we’ll put everything together and build a simple calculator. Let’s break this down step by step!
1. Getting User Input in PHP
To get input from the user, we first need to create an HTML form and then process that input using PHP. The most common methods for collecting data are GET
and POST
.
<!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>
Creating the PHP File (site.php)
In this step, we will create a PHP file named site.php
. This file will handle the form submission when the user clicks the Submit button, as mentioned in your previous HTML form.
Here’s how you can structure the code in site.php to process and display the submitted form data.
<?php
echo $_GET["name"];
?>
Declaring Methods in PHP: GET vs POST
In PHP, there are two primary methods for submitting form data to the server: GET and POST. Both methods have their advantages and use cases, but they differ significantly in how they send data and the security implications of using them. Let’s look at both methods in detail.
1. GET Method
- How it works:
The GET method sends form data as part of the URL. The data is appended to the URL in the form of query parameters (e.g.,?name=John&age=30
). - Advantages:
- It’s visible in the URL, so it’s useful for sending small amounts of data (such as search queries or filters).
- It’s simple to implement.
- Users can bookmark or share data via a URL.
- Disadvantages:
- Security risk: Since the request appends the data to the URL, anyone who views the URL (e.g., in browser history or logs) can see the data.
- Limited data size: Most browsers and servers limit the length of the URL (around 2000 characters), so GET is not suitable for sending large amounts of data.
2. POST Method
- How it works:
The POST method sends form data as part of the HTTP request body, not the URL. This method is more secure because the data is not visible in the URL. - Advantages:
- Better Security: Since the request sends the data in the body, it keeps the data hidden from the URL, making it less visible to potential interceptors. However, it can still be vulnerable to other attacks such as XSS (Cross-Site Scripting) and CSRF (Cross-Site Request Forgery).
- Suitable for sending large amounts of data (e.g., form submissions, file uploads).
- The browser does not cache or save the data in history.
- Disadvantages:
- Unlike the GET method, users cannot bookmark or share data via a URL.
- Let me know if you’d like any more adjustments!
- Slightly more complex to implement because the server needs to process the request body.
<?php
$color = $_GET["color"];
?>
Understanding Arrays in PHP
An array in PHP is a data structure that allows you to store multiple values in a single variable. These values can be of any type, such as integers, strings, or even other arrays.
In PHP, arrays start counting from 0, meaning the first element of an array has the index 0, the second has index 1, and so on.
Types of Arrays in PHP
There are two types of arrays you can work with in PHP:
- Indexed Arrays: Arrays with numerical indices (0, 1, 2, etc.).
- Associative Arrays: Arrays where you assign custom keys (strings) to values
<?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
A block of code defines a function in PHP, and you can reuse it multiple times after defining it. It allows you to organize your code, improve modularity, and reduce redundancy, making it easier to maintain and update.
Defining and Calling a Function
1. Defining a Function
A function is defined using the function
keyword, followed by the function name and parentheses. The code block inside the function is wrapped in curly braces {}
.
2. Calling a Function
After defining a function, you can call it by using its name followed by parentheses.
2. Calling a Function
After defining a function, you can call it by using its name followed by parentheses.
<?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>
Thank for all! practice well and try coding guys
See you Next php Lesson Soon!