I don't understand how it works a function.
I don't understand the parameters are different with the function and the call of the function.

there are the function and the call of the function
the function is function create_table($data) and the call of the function is create_table($my_data);

why the function, function create_table() the variable is not $my_data.
I have one array $my_data, pass the parameters to the array with the function create_table($my_data)

<!DOCTYPE html>
<html>
<head>
    <style type="text/css">
        table, tr, td {
            border: 1px solid black;
            border-collapse: collapse;
            padding: 3px;
        }
    </style>
    <title>Create Table</title>
</head>
<body>
<?php
function create_table($data) {
    echo '<table>';
    reset($data);
    $value = current($data);
    while ($value) {
        echo "<tr><td>$value</td></tr>\n";
        $value = next($data);
    }
    echo '</table>';
}

$my_data = ['First piece of data','Second piece of data','And the third'];
create_table($my_data);
?>
</body>
</html>

I think is better to change $my_data for $data

bertrc why the function, function create_table() the variable is not $my_data.

I contract someone to do a job for me, and I send them some "stuff". I call it one thing, but they, with their specialist knowledge of whatever it is I'm contracting them for, might call it something else. Doesn't matter what it's called: it's still the same stuff.

What gets sent to a function is a value, not the variable that contains the value. (The technical name for that value is that it's the function's "argument".) The first thing the create_table function does when you call it is set $data to be ['First piece of data','Second piece of data','And the third']. (This is called "pass by value"; there are alternatives: such as "pass by reference" and the one you're thinking of which is called "pass by name". But PHP uses pass-by-value, which is probably the commonest, so that's what I'm talking about here.)

You could write the call as

create_table(['First piece of data','Second piece of data','And the third']);

And then what would you expect the parameter to be named then?

Or think about the case where you want to call the function more than once

create_table($first_table);
create_table($second_table);
create_table($third_table);

How would that work if all three variables had to have the same name?

Or look at the manual, where you have, for example, strpos($haystack, $needle). You aren't expected to call the variables you're passing to it "haystack" and "needle", you're expected to call them names that make sense in your program. The writers of strpos don't know your program so they don't know what names you'd use, they used names that made sense inside strpos.

And here's a thing to think about: if you had to use the same variable both in the function and when you called it ("pass by name"), you wouldn't need to pass it in the first place: the function would already have the variable (this is basically how GO SUB worked in old BASIC).

(And, just as an aside: why the incredibly ancient loop inside the function? foreach has been a thing since PHP 3.)

(As a second aside: if you really want readers of your code to know what parameter you're passing a value to you can write create_function(data: $my_data);.)

(And a third aside: PHP does provide a mechanism for pass-by-reference, and it's still occasionally used; that's when you put the & next to the parameter when you define the function.)

    Write a Reply...