Although I've read about it it still doesn't sink in when you will need to pass in a variable. I think I know how i just don't know for what reason. If someone could provide that information for me i would appreciate it. Also, is PHP variable passing more efficient than doing so in HTML? Thanks for your time and help.

Dani AI

Generated

A simple way to think about variable passing is to separate scope from persistence. asked about moving data from page to page; covered the local scope case (function parameters) and showed one client-side transport. For short-lived, per-request values prefer explicit parameters or local variables to keep code testable. For state that must survive a redirect or multiple requests, choose between GET/POST, cookies, server-side sessions, or persistent storage (database) depending on sensitivity and lifetime.

Which method to use and why:

  • GET is for resource identifiers and bookmarkable filters; it is visible and modifiable by the user (see $_GET).
  • POST is for form submissions and larger payloads ( $_POST).
  • Sessions hold per-user server-side state without exposing values to the client (see sessions).
  • Cookies are small, client-side and persistent; do not store secrets (setcookie).
    Always treat client data as untrusted. Use filter_input and prepared statements, and regenerate session IDs after login with session_regenerate_id.

A minimal pattern for carrying transient state server-side:

<?php
session_start();
$_SESSION['flow_data'] = $_POST['step_value'] ?? null;
header('Location: next.php');
exit;
?>

Troubleshooting pointers: if a variable is missing, check the form method and name, ensure session_start() runs before output, call setcookie()/header() before any HTML, and always validate incoming values.

Recommended Answers

All 4 Replies

In what context are you referring?
Functions ?

function myFunction($var1, $var2){
return ($var1 + $var2);
}

In the example above I passed var1 and var2 into a function that adds them together and returns the result.

//call this function
$total =myFunction(1,2);

I passed 'hard coded' numbers here but variables could be used also

$a = 1;
$b =2;
$total =myFunction(a,b);

both will return 3 to $total

In what context are you referring?
Functions ?

function myFunction($var1, $var2){
return ($var1 + $var2);
}

In the example above I passed var1 and var2 into a function that adds them together and returns the result.

//call this function
$total =myFunction(1,2);

I passed 'hard coded' numbers here but variables could be used also

$a = 1;
$b =2;
$total =myFunction(a,b);

both will return 3 to $total

I'm sorry. I should have been more clear. I meant from one page to another.

So you are referring to something like this? <a href="whatever.com?variable=value">Example</a>

There are a lot of reasons, but the main reason I use them is to capture something specific in order to use it later. For example, say you've got a forum like daniweb. Each thread is essentially assigned an id to identify it. If a user is not logged in and tries to reply, they would be redirected to a login page first. But you need to keep track of the thread they were at. So you would pass a variable so the redirection script knows where they were initially to take them back there after log in. This is just one example of course.

So you are referring to something like this? <a href="whatever.com?variable=value">Example</a>

There are a lot of reasons, but the main reason I use them is to capture something specific in order to use it later. For example, say you've got a forum like daniweb. Each thread is essentially assigned an id to identify it. If a user is not logged in and tries to reply, they would be redirected to a login page first. But you need to keep track of the thread they were at. So you would pass a variable so the redirection script knows where they were initially to take them back there after log in. This is just one example of course.

Nice example. I welcome others with a little code illustration. Thanks everyone.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.