Page 1 of 1
Simple Variable Question
Posted: Tue Dec 19, 2006 11:40 am
by Superman859
I was wondering if variables (specifically when using $query and running it on a MySQL Database) carry into the next PHP 'segment'.
What I mean by this is the following...
Code: Select all
<?php
//$query = "SELECT * FROM site_info WHERE pagename = '$filename'";
//$result = mysql_query($query);
//$row = mysql_fetch_row($result);
echo $row[5];
?>
//Some Random HTML Code
<?php
echo $row[7]; // Can I do this, or do I need to run the query again? It works, but is it standard and should work at all times? Or do variables clear after you close a PHP section?
?>
Posted: Tue Dec 19, 2006 11:44 am
by feyd
Did you try it?
Re: Simple Variable Question
Posted: Tue Dec 19, 2006 12:11 pm
by nhammond
Superman859 wrote:I was wondering if variables (specifically when using $query and running it on a MySQL Database) carry into the next PHP 'segment'.
What I mean by this is the following...
Code: Select all
<?php
//$query = "SELECT * FROM site_info WHERE pagename = '$filename'";
//$result = mysql_query($query);
//$row = mysql_fetch_row($result);
echo $row[5];
?>
//Some Random HTML Code
<?php
echo $row[7]; // Can I do this, or do I need to run the query again? It works, but is it standard and should work at all times? Or do variables clear after you close a PHP section?
?>
It will work just fine unless you navigate to another page. That's the nice thing about php is you can jump in and out of code although you should try to keep some structure and keep it clean so it is easy to read. Your example though is typical of displaying a page with quite a few dynamic features. If you end up with mulitple queries though you are going to need to be more specific with your row names such as row_names, row_city blah blah
Posted: Tue Dec 19, 2006 12:57 pm
by boo_lolly
feyd wrote:Did you try it?
you can learn a LOT by trying it. listen to feyd.
Posted: Tue Dec 19, 2006 1:00 pm
by Superman859
I did try it - that's how I knew it works if you read the original post.
I was just double checking to be sure it's proper to do so - I've been doing a lot of Flash coding lately that was working sometimes and wasn't working others - had a lot of trouble figuring out whether something was wrong with the Flash or the Javascript...there was a problem, yet on my computer it was working without any problem.
Just wanted to double check for PHP since I don't use it quite as often.
Posted: Tue Dec 19, 2006 1:36 pm
by RobertGonzalez
Code: Select all
<?php
$this_var = 'doggiebones';
/*
The $this_var variable will stay unchanged until
A. You change it directly by setting it to another value
B. The script changes it for you using some type of logic
C. The script ends (it essentially evaporates)
*/
var_dump($this_var);
// Lets change it now
$this_var = 12;
var_dump($this_var);
// and one more time
$this_var = array('cat', 'dog', 'banana sandwich');
var_dump($this_var);
?>
Posted: Tue Dec 19, 2006 1:54 pm
by izak30
yes, variables maintain between segments, and so do if statments (maybe not as obvious
Code: Select all
<?php
if(x=x)
{
do this;
?>
<html>///whatever
<?php
also do this;
}
?>
is completly ok, maybe not the easiest to read, but it's completely acceptable