Simple Variable Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Superman859
Forum Commoner
Posts: 47
Joined: Sun Oct 29, 2006 10:22 am

Simple Variable Question

Post 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?
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Did you try it?
nhammond
Forum Newbie
Posts: 14
Joined: Mon Dec 18, 2006 11:35 am

Re: Simple Variable Question

Post 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
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post by boo_lolly »

feyd wrote:Did you try it?
you can learn a LOT by trying it. listen to feyd.
Superman859
Forum Commoner
Posts: 47
Joined: Sun Oct 29, 2006 10:22 am

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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);
?>
izak30
Forum Newbie
Posts: 5
Joined: Tue Dec 19, 2006 1:29 pm

Post 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
Post Reply