Tuesday, April 22, 2025

PHP Function Arguments

You are advised to check corresponding YouTube video at the end of this article.

The terms "parameter" and "argument" are often used interchangeably, but there is a slight difference between them.

A parameter is a variable or a placeholder in a function definition that represents the input that the function expects. It is a part of the function's signature and defines the type and order of the input that the function expects.

An argument, on the other hand, is the actual value that is passed into a function when it is called. It is the data that is supplied to the function for it to work on.

To put it simply, a parameter is what a function expects, while an argument is what a function receives.

The reason why we pass arguments to functions is to provide input data to the function so that it can perform a task based on that input data. For example, if you are writing a function that calculates the area of a rectangle, you would need to pass the length and width of the rectangle as arguments to the function so that it can perform the calculation.

Passing arguments to functions makes your code more flexible and reusable, as you can use the same function to perform the same task with different input data. It also helps to reduce code duplication, as you can create a function that performs a task and then call that function multiple times with different arguments.

In addition, passing arguments to functions allows you to separate the input data from the logic of the function. This makes your code easier to read, understand, and maintain, as the function only needs to focus on performing the task, while the input data is handled separately. 


<?php 

function one_ping($host) {
	echo "<pre>";
	system ("ping ". $host);
	echo "</pre>";
}

one_ping("google.com");

?>

This is a PHP code that defines a function called "one_ping" and then calls it with the argument "google.com".

The function "one_ping" takes one parameter, which is the hostname or IP address of the host that you want to ping. The function uses the "echo" statement to print the opening "<pre>" tag, which formats the output as preformatted text, and then uses the "system" function to execute the "ping" command on the specified host. The output of the "ping" command is then printed to the screen.

The "ping" command sends a network packet to the specified host to test the network connection and measures the time it takes for the packet to be sent and received. The output of the "ping" command shows the number of packets sent and received, the percentage of packet loss, and the time it took to send and receive each packet.

The last line of the code calls the "one_ping" function by using its name followed by parentheses and passing the argument "google.com", which executes the function and prints the output to the screen. In this case, the output will be the result of the "ping" command on the host "google.com".

By defining a function, you can reuse the code to ping different hosts by passing different arguments to the function. This makes the code more modular and easier to maintain.

form.html


<form action="grabthings.php" method="post">
<label>Host to Ping:</label>
<input type="text" name="host">
<br><br>

<input type="submit" value="Submit">
</form>

This is an HTML code that creates a form with a text input field and a submit button.

The form uses the "post" method to send the data to the "grabthings.php" file when the user clicks the submit button. The "action" attribute specifies the URL of the file that will handle the form submission.

The form contains a label that says "Host to Ping" and a text input field with the name "host". When the user types a value into the text field and submits the form, the value will be sent to the server as a POST parameter with the name "host".

The submit button has the text "Submit" and will submit the form when clicked.

grabthings.php


<?php 

$get_host = $_POST["host"];

function one_ping($x) {
	echo "<pre>";
	system ("ping ". $x);
	echo "</pre>";
}

one_ping($get_host);

?>

This is a PHP code that retrieves the value of the "host" parameter from the form submission using the $_POST superglobal variable, assigns it to the variable $get_host, and then calls the "one_ping" function with $get_host as the argument.

The "one_ping" function takes one parameter, which is the hostname or IP address of the host that you want to ping. The function uses the "echo" statement to print the opening "<pre>" tag, which formats the output as preformatted text, and then uses the "system" function to execute the "ping" command on the specified host. The output of the "ping" command is then printed to the screen.

By passing the value of the "host" parameter to the "one_ping" function as an argument, the function will ping the host that was entered into the text field on the form submission.

Note that this code assumes that the form was submitted using the "post" method and that the name attribute of the text input field is "host".

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...