Stuck in endless "Parsing error loop"


#1

Dear troubleshooters:

For the Lesson: Practice Makes Perfect...

Please decipher:

Error: Missing argument 2 for aboutMe(), called on line 15 and defined (line 10)Undefined variable: age (line 11) Hello! My name is Gregory, 59 and I am years old.

Code:

<?php
$name = 'Gregory'; //This is line 9.
$age = "59"; //I also tried the statement without the "", i.e. $age = 59;
function aboutMe($name, $age) {
echo "Hello! My name is " . $name . " and I am " . $age . " years old.";
}
?>
<?php
aboutMe("$name, $age");
?>


#2

Got it to work.

I removed the quotes on the parameters in the call.

$name = 'Gregory';
$age = 59;
function aboutMe($name, $age) {
    echo "Hello! My name is " . $name . " and I am " . $age . " years old.";
}
?>
<?php
    aboutMe($name,$age);
?>

#3

Confused about when I need to use "" in the parameters function() when either using built-in or user-defined functions that have text items. Are there any general rules of thumb?


#4

Yup.There are simple rules for function calling and also for the usage of (" ").Below are the two types of different codes to get the right answer.

<p>
        <?php
        function aboutMe($name,$age){
            echo "Hello! My name is $name, and I am $age years old";}
            aboutMe("Soundharya",22);//Calling function
        ?>
</p>

or

<p>
        <?php
        function aboutMe($name,$age){
            echo "Hello! My name is $name, and I am $age years old";}
             $n="Soundharya";$a=22;
            aboutMe($n,$a);//Calling function
           ?>
      </p>
  • You have to use double quotes(" ") for string alone and for numbers you should not use either the single or double quotes.
  • In first prgm, I have directly assigned the values for the arguments and the passed through the function.
  • In second prgm,I have assigned the values for the arguments separately by using the different variables than which are used in function definition.You can observe this by these lines function aboutMe($name,$age) and aboutMe($n,$a);..While using this method, you have assign the values for the variable outside the function definition which is aboutMe($name,$age).

Hope this helps you.