PHP Variable Scope Between Code Blocks

PHP variable scope between code blocks

You're putting too much meaning in the php code blocks.

It's not something that global.

These blocks belong to the same PHP script. It's just a neat way to output HTML, nothing more. You can substitute it with echoing the HTML and there will not be the slightest difference.

The whole PHP script is being executed at once, not in iterations, as you probably picture this, thinking that PHP blocks are being executed server-side, then HTML blocks client-side, and then back to PHP blocks on the server side and so on. That's wrong.

The whole PHP script is being executed on the server side, resulting with pure HTML in the browser, and then dies.

That's why you can't program both an HTML form and its handler in the same PHP script by just placing the latter one right after the former. You have to make another call to the server to make the handler work. It will be another call completely, another instance of the same script, knowing nothing of the previous call which is long dead already. And that's another thing you have to know about PHP:

PHP script execution is atomic. It's not like a desktop application constantly running in your browser, or even a daemon with persistent connection to your desktop application. It's more like a command-line utility - doing its job and exits. It runs discretely:

  1. a browser makes a call
  2. PHP wakes up, creates an HTML page, sends it to the browser and dies
  3. Browser renders that HTML and shows it to the user.
  4. User clicks a link
  5. a browser makes a call
  6. another PHP instance, knowing nothing of the previous call, wakes up and so on

How is it possible to reference to a variable outside of a code block when it is declared inside a block?

Now my understanding is that variables that are declared in a block of code are only able to be used in that block

Wrong, if you mean {} as a block.

According to PHP Manual

Any variable used inside a function is by default limited to the local function scope.

There is no mention of {} level scope within a function. Any variable declared inside a function is available throughout it, even if it as declared inside any sub braces. That's why its still available. Your variables like $un can be accessed even outside the loop, just that they will contain the values from last iteration.

Variable scope difference between PHP and C: block scope is not exactly the same?

PHP only has function scope - control structures such as if don't introduce a new scope. However, it also doesn't mind if you use variables you haven't declared. $i won't exist outside of main() or if the if statement fails, but you can still freely echo it.

If you have PHP's error_reporting set to include notices, it will emit an E_NOTICE error at runtime if you try to use a variable which hasn't been defined. So if you had:

function main() {
if (rand(0,1) == 0) {
$i = 3;
}
echo $i;
}

The code would run fine, but some executions will echo '3' (when the if succeeds), and some will raise an E_NOTICE and echo nothing, as $i won't be defined in the scope of the echo statement.

Outside of the function, $i will never be defined (because the function has a different scope).

For more info: http://php.net/manual/en/language.variables.scope.php

PHP variable scope within Try/Catch block

Your code is valid. Variable scope in PHP is by function, not block. So you can assign a variable inside the try block, and access it outside, so long as they're in the same function.

access to variables from different php blocks

You should look in to something like a MVC development pattern.

The easiest way would be not to output everything in many blocks, but to append to a certain variable which is echoed at the end.

// code block
$html .= '<strong>Some html</strong>';

// other code block
$html .= '...';

// Other code

// At last, at the end
echo $html;

PHP global variables between tags

Just use echo $value.

The variable context doesn't change just because you have re-opened the PHP tag. <?php and ?> are just flags for the parser, and have no bearing on what your code does inside of them.

Since you're just getting started, I also recommend looking into a templating engine such as Smarty. This will help you separate application logic from your output. Also, be sure to use htmlspecialchars() around any arbitrary data used in the context of HTML, to ensure that reserved characters are escaped, and that you aren't creating any XSS attack points.



Related Topics



Leave a reply



Submit