Jump to content

[SOLVED] if(){}if(){}if(){}else{}if(){}return False;


Recommended Posts

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

Link to comment
Share on other sites


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.

 

Link to comment
Share on other sites

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,

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

:/

Link to comment
Share on other sites

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;
         }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.