Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 13th, 2007, 06:46 PM
Registered User
 
Join Date: Dec 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default conditionally setting variable

Hi all,

I am trying to make some php code that executes a function if a variable has the right value, and to give that variable a value in a number of conditional statements (if I get the terminology right).

To do this I started trying to set variable 1 and then variable 2, and then to change variable 2 dependent on what variable 1 is, and finally execute the function if variable 2 has the right value.

Here is the code:
Code:
textfunction() {
    echo "text";
}
$moap_inexclude = 'module';
var $callModornot; //from here
if ($moap_inexclude == "module") {
    global $callModornot = 1;
} //to here
if (isset($callModornot) && $callModornot) {
    dotextfunction();
}
Unfortunately I cannot get this to work. I know the last conditional statement and the function work because tested by simply setting callModornot to 1.
In reality I am getting moap_inexclude from a xml file and I was also able to test that the code gets the value from that xml file.
So the problem must be in the lines I commented with from here/to here.
I thought that it might have something to do with whether or not the variable is available in a conditional statement and read a bit about the scope of local and global variables but that does not seem to apply here.

Does anyone have any idea what I am doing wrong?
Reply With Quote
  #2 (permalink)  
Old December 13th, 2007, 07:09 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Oklahoma City, Oklahoma, USA.
Posts: 249
Thanks: 0
Thanked 0 Times in 0 Posts
Default

1. In your function you need to add the word "function" or this will not work.
2. It is a good idea to go ahead and assign $callModornot a value you would test it and assign the value, with the "else" statement.

function textfunction() {
    echo "text";
}

$moap_inexclude = 'module';

if ($moap_inexclude == 'module') {
    $callModornot = 1;
} else {
    $callModornot = 0;
}

if ($callModornot) {
    textfunction();
}

I am not sure what you are using $callModornot for but another way you could do this is and cut down on your conditional statements is.

function textfunction() {
    echo "text";
}

$moap_inexclude = 'module';

if ($moap_inexclude == 'module') {
    $callModornot = 1;
    textfunction();
} else {
    $callModornot = 0;
}
Reply With Quote
  #3 (permalink)  
Old December 13th, 2007, 07:39 PM
Registered User
 
Join Date: Dec 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your swift reply!

Ad 1. You're right of course, and in the actual code the word "function" is there.

Ad 2. The if-else did the trick, thanks! - I hope it will continue working as I add additional conditions, see next point..

Ad 3. What I am working towards is testing against a number of conditions to see if the function should be executed. I thought the best way was to declare $callModornot and then use conditional statements to determine if it should be 0 or 1.
Basically I have three scenarios. In the first, when the value of $moap_inexclude is 'module', the function should simply execute. In the second scenario, when $moap_inexclude has a value of 'exclude', the idea is to set $callModornot to 1 and then test against a number of conditions to see if it should be set to 0. The other way around is the third scenario, when the value of $moap_inexclude is 'include', which sets $callModornot to 0 and then tests against a number of conditions to see if it should be set to 1.
I do not know if there is a smarter way to do this, but to illustrate here is the current state of progress with the second scenario:
Code:
if ($moap_inexclude == "exclude") {
    $callModornot = 1;
    if (
    ($moap_view == 0 && ($option="com_content" && $task="view")) ||
    ($moap_blogcat == 0 && ($option="com_content" && $task="blogcategory")) ||
    ($moap_cat == 0 && ($option="com_content" && $task="category")) ||
    ($moap_blogsect == 0 && ($option="com_content" && $task="blogsection")) ||
    ($moap_sect == 0 && ($option="com_content" && $task="section")) ||
    ($moap_content == 0 && ($option="com_content")) ||
    ($moap_frontcont == 0 && ($option="com_content" || $option="com_frontpage"))
    ) {
        $callModornot = 0;
    }
}
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem setting a variable jtrifts Javascript 4 August 9th, 2007 08:36 AM
Newbie : How do you .. without setting a variable rqaran XSLT 2 August 6th, 2005 04:09 AM
declare a variable without setting a value to it crmpicco Javascript How-To 1 July 18th, 2005 07:25 PM
setting each value from database as a variable Ashleek007 Beginning PHP 21 April 18th, 2005 03:50 PM
Setting a DTS Global Variable ioates SQL Server 2000 0 October 23rd, 2003 04:28 AM



All times are GMT -4. The time now is 05:30 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.