Rebelrebellious Posted May 28, 2008 Share Posted May 28, 2008 Can if statements be used one after another wihout an else? For example. Are the following two examples equivelant? if(A == 0) { $a = 'A1False'; } else { $a = 'B'; if(A > 1) { $a .= '2'; if(A > 2) { $a .= 'True'; } else { $a .= 'False'; } } else { $a .= 'False'; } } return $a; if(A == 0) {$a = 'A1';} else {$a = 'B';} if(A > 1) {$a .= '2';} if(A > 2) {$a .= 'True';} else {$a .= 'False';} return $a; possible outcomes : A1False, BFalse, B2True, B2False Quote Link to comment https://forums.phpfreaks.com/topic/107594-solved-ifififelseifreturn-false/ Share on other sites More sharing options...
conker87 Posted May 28, 2008 Share Posted May 28, 2008 Of course. Quote Link to comment https://forums.phpfreaks.com/topic/107594-solved-ifififelseifreturn-false/#findComment-551473 Share on other sites More sharing options...
Rebelrebellious Posted May 28, 2008 Author Share Posted May 28, 2008 function get_header_contents(/* [$headerfile], [$title], [$bodyclass] */) { $argcount = func_num_args(); if($argcount == 0) { announce('Using the default header.'); $header = '<html><head><title>'.TITLE.'</title></head>'; } else { announce('Getting the header.'); $actualcontents = file_get_contents(func_get_arg(0)); $strlen = strpos($actualcontents, '<body>'); $header = substr($actualcontents, 0, $strlen); } if($argcount > 1) { announce('Setting the title.'); $beginning = strpos($header, '<title>') + 7; $strlen = strpos($header, '</title>') - $beginning; $oldtitle = substr($header, $beginning, $strlen); $newtitle = func_get_arg(1); $header = str_replace($oldtitle, $newtitle, $header); } if($argcount > 2) { announce('Setting the body class.'); $header .= '<body class="'. func_get_arg(2) .'">'; } else { announce('Using the default body class.'); $header .= '<body class="default">'; } return $header; } Can you spot the error then? My header is coming out somewhat garbled. My intention is to get the header and the opening body tag and optionally modify them a bit. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/107594-solved-ifififelseifreturn-false/#findComment-551477 Share on other sites More sharing options...
kev wood Posted May 28, 2008 Share Posted May 28, 2008 you may be better looking at the switch statement. if you are going to have a lot of if statements then this will work better and my help you work your problem out a bit easier. Quote Link to comment https://forums.phpfreaks.com/topic/107594-solved-ifififelseifreturn-false/#findComment-551480 Share on other sites More sharing options...
runnerjp Posted May 28, 2008 Share Posted May 28, 2008 use if first then use elseif Quote Link to comment https://forums.phpfreaks.com/topic/107594-solved-ifififelseifreturn-false/#findComment-551490 Share on other sites More sharing options...
Rebelrebellious Posted May 28, 2008 Author Share Posted May 28, 2008 If you look at my initial post you will see that the example scenario has the same structre as the actual problem. I want all of the if statements to be tested, even if the previous if was true. My intention was to take the least verbose approach. My suspicion is that there may be a problem with the str_replace or the argcount test. No luck so far, but I'll keep trying to figure it out. Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/107594-solved-ifififelseifreturn-false/#findComment-551492 Share on other sites More sharing options...
Rebelrebellious Posted May 29, 2008 Author Share Posted May 29, 2008 I'm bumping this up again, maybe a fresh set of eyes can see what I am missing. Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/107594-solved-ifififelseifreturn-false/#findComment-552695 Share on other sites More sharing options...
.josh Posted May 29, 2008 Share Posted May 29, 2008 Okay well I'm afraid I can't personally make sense of your code's intentions without seeing some example data (like, what you are feeding it vs. what it's supposed to look like, etc...) so I'm gonna kind of see if I can throw out there how I see the logic of the actual code. Maybe you can confirm or deny it or whatever. I see it like this: A - 1 - if arg = 0 do this 2 - if arg != 0 do this B - if arg > 1 do this C - 1 - if arg > 2 do this 2 - if arg <= 2 do this so let's use example numbers here: arg = 0 A - 1 - if arg = 0 do this : executed 2 - if arg != 0 do this : not executed B - if arg > 1 do this : not executed C - 1 - if arg > 2 do this : not executed 2 - if arg <= 2 do this : executed arg = 1 A - 1 - if arg = 0 do this : not executed 2 - if arg != 0 do this : executed B - if arg > 1 do this : not executed C - 1 - if arg > 2 do this : not executed 2 - if arg <= 2 do this : executed arg = 2 A - 1 - if arg = 0 do this : not executed 2 - if arg != 0 do this : executed B - if arg > 1 do this : executed C - 1 - if arg > 2 do this : not executed 2 - if arg <= 2 do this : executed arg = 3 A - 1 - if arg = 0 do this : not executed 2 - if arg != 0 do this : executed B - if arg > 1 do this : executed C - 1 - if arg > 2 do this : executed 2 - if arg <= 2 do this : not executed arg > 3 : same as arg = 3 Quote Link to comment https://forums.phpfreaks.com/topic/107594-solved-ifififelseifreturn-false/#findComment-552716 Share on other sites More sharing options...
Rebelrebellious Posted May 29, 2008 Author Share Posted May 29, 2008 That is it precisely. All of the arguments are optional. If no arguments are provided a barebones html header is used. The headerfile/func_get_arg(0) is a regular html file that works properly. Title/func_get_arg(1) is a string that should be placed between the title tags, replacing the original title. The third optional argument changes the class of the opening body tag. In my php, I am creating this header then printing it. After that I print some other stuff and end by printing the closing body and html tags. If there is an error in the php, I intend to simply grab the entire contents of the html file and print them. At runtime this is the call. (SAFE is False) if(SAFE == True) {print get_header_contents();} else {print get_header_contents(HTMLPAGE, TITLE);} this is the output: <body class="default">YPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <link media="print" rel="stylesheet" type="text/css" href="npprint.css" /> <link media="screen" rel="stylesheet" type="text/css" href="npscreen.css" /> <title>The original unmodified title</title> </head> <body class="default"> So the the header is getting put in, the title is not gettng changed. The default body is being used twice, and one of the times is in the wrong place. I thought that the problem may have been due to the way I am using the if statements but now I think that the problem likely lies with the string replace. Honestly, I wasn't here to look at the script yesterday, otherwise I may have found the solution by now. I will be working on it again today, and I figured that it didn't hurt to ask for help again. I appreciate that you are looking into this with me. Quote Link to comment https://forums.phpfreaks.com/topic/107594-solved-ifififelseifreturn-false/#findComment-552747 Share on other sites More sharing options...
Rebelrebellious Posted May 29, 2008 Author Share Posted May 29, 2008 I found an error just now in a different part of the script. It has been corrected, and no changes have been made to the script we have been discussing. The current output from the script we are discussing is simply: <body class="default"> So I am still at a loss as to why the default body tag is being printed and nothing else. As if the number of arguments was not 0, 1, or 2, but ye it was less than 2. :/ Quote Link to comment https://forums.phpfreaks.com/topic/107594-solved-ifififelseifreturn-false/#findComment-552757 Share on other sites More sharing options...
Rebelrebellious Posted May 29, 2008 Author Share Posted May 29, 2008 Okay. I fixed it. Here is the working code. See line 13 fo the error. Thank you everyone. function get_header_contents(/* [$headerfile], [$title], [$bodyclass] */) { $argcount = func_num_args(); if($argcount == 0) { announce('Using the default header.'); $header = '<html><head><title>'.TITLE.'</title></head>'; } elseif($argcount != 0) { announce('Getting the header.'); $actualcontents = file_get_contents(func_get_arg(0)); $strlen = strpos($actualcontents, '<body'); // The body tag was not being found because it is <body class="default"> $header = substr($actualcontents, 0, $strlen); } if($argcount > 1) { announce('Setting the title.'); $beginning = strpos($header, '<title>') + 7; $strlen = strpos($header, '</title>') - $beginning; $oldtitle = substr($header, $beginning, $strlen); $newtitle = func_get_arg(1); $header = str_replace($oldtitle, $newtitle, $header); } if($argcount > 2) { announce('Setting the body class.'); $header .= '<body class="'. func_get_arg(2) .'">'; } else { announce('Using the default body class.'); $header .= '<body class="default">'; } return $header; } Quote Link to comment https://forums.phpfreaks.com/topic/107594-solved-ifififelseifreturn-false/#findComment-552761 Share on other sites More sharing options...
.josh Posted May 29, 2008 Share Posted May 29, 2008 ahh dammit you know i noticed that very thing and I almost posted something about it but I wasn't sure since I didn't know the context of your data and then I got distracted. GJ! Quote Link to comment https://forums.phpfreaks.com/topic/107594-solved-ifififelseifreturn-false/#findComment-552782 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.