discardthis
discardthis

Reputation: 179

Why is this variable not parsed when placed inside a php function?

This code displays the html form but without a value (the submit box will be empty) generated by a mysql query.

<?php
function form() {
    echo "<form>
              <form action=\"page.php\" method=\"post\">
              <input type=\"text\" name=\"name\" value=\"$row[column]\">
              <input type=\"submit\">
          </form>";     
}

form();
?>

However, if I echo the following when not in a function the $row[column] array is echoed correctly:

<?php
echo "<form>
          <form action=\"page.php\" method=\"post\">
          <input type=\"text\" name=\"name\" value=\"$row[column]\">
          <input type=\"submit\">
      </form>";         
?>

The two codes are identical, except one is in a function. How do I get the array to be echoed when inside a function? What am I doing wrong?

Upvotes: 0

Views: 63

Answers (5)

Mike Brant
Mike Brant

Reputation: 71384

Your problem is one of variable scoping. $row exists in the global scope. From inside the function, the globally-scoped vairables are not available.

Here is link for further reading - https://www.php.net/manual/en/language.variables.scope.php

To resolve this, you must make $row available to the function. There are generally two ways to do this. The typically preferred way, is to inject the dependent variable into the function, by passing it as parameter:

function form($row) {
    // work with $row inside function
}

This is known as "dependency injection".

A second way (which really should not be used, but I show it here because you may encounter it in existing code/code examples) would be to simply use the global keyword to make the global variable available in function scope:

function form() {
    global $row;
    // work with $row inside function
}

Upvotes: 4

mpyw
mpyw

Reputation: 5754

Why don't you use Here Document?

<?php

function form($value) {
// Do not forget to escape input value
$value = htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
echo <<<EOD
<form action="page.php" method="post">
<input type="text" name="name" value="{$value}">
<input type="submit">
</form>  
EOD;
}

form($row['column']);

Upvotes: 0

G&#225;bor H&#233;ja
G&#225;bor H&#233;ja

Reputation: 495

If you'd really like to put the variable there you should use $GLOBALS, also you should use the . operator instead of + to concatenate strings, so it is ... value=\"" . $GLOBALS['row']['column'] . "\"> ....

Although using global values in functions is not best practice approach (to say the least), especially when a straight print is using them - see strip_tags(), htmlspecialchars(), htmlentities() and their friends.

Instead of using global variable you should use function parameters, so in your case it is:

<?php
  function form($row) {
      echo "... " . htmlspecialchars($row['column']) . " ...";
  }

  form($row);
?>

Upvotes: 0

RyanS
RyanS

Reputation: 4194

You must have $row defined somewhere outside of the function scope. Try this:

<?php
function form( $row ) {
    echo "<form>
              <form action=\"page.php\" method=\"post\">
              <input type=\"text\" name=\"name\" value=\"$row[column]\">
              <input type=\"submit\">
          </form>";     
}

form( $row );
?>

Upvotes: 2

Jason OOO
Jason OOO

Reputation: 3552

Because of different scope between variables inside function and outside function, take it easy, variables inside function are not same as outside, for example: if you defined one variable outside function, its not accessible inside function by usual way, instead, you can use function parameter.

Upvotes: 0

Related Questions