# Php - Simple php help please



## Nordic (Jun 12, 2012)

I am trying to get update a page text by the variable. I know I am doing this wrong. I am not that good at php but am using php for this project I am on. I am pretty sure I am using the while function wrong. I have tried googling this but can't find an answer because I am not sure what I am doing wrong.

*So the two things I really need help with are how to properly do while (251 < $variable  <= 350). This way doesn't work.
And how to end the while loop so it does not repeat. That endwhile causes and error.*
I know these are pretty simple. I just can't remember how to do it.


```
<?php
$variable = 50

$file0 = file_get_contents("file0.txt");
$file1 = file_get_contents("file1.txt");
$file2 = file_get_contents("file2.txt");


while ($variable <= 250) {
    echo $file0;
	}
while (251 < $variable  <= 350) {
    echo $file1;
	}
while (351 < $variable  <= 500) {
    echo $file2;
	}
endwhile;
?>
```


----------



## Ev1LrYu (Jun 12, 2012)

should it be something like get_file_contents()?


----------



## Nordic (Jun 12, 2012)

Ev1LrYu said:


> should it be something like get_file_contents()?



That may be... I will try that too. Right now it isn't liking the second while function. That is really what I am asking about. Thank you for pointing that out though. That is probably another problem I would of came across later.

Edit:
This does do a better job than include


----------



## Ev1LrYu (Jun 12, 2012)

oh btw, I don't think you are using 'while' correctly. 'while' is used for doing loops, ex:

//counting to ten
$i = 1; //start with 1
while ($i <= 10){ //test if less than or equal to 10
echo $i."<br />"; //if it is, print it out
$i++; // increase the counter
}

If I understand you correctly then you need to use the If/then/else control structure as documented for php here: http://php.net/manual/en/control-structures.elseif.php


----------



## W1zzard (Jun 12, 2012)

i'm having a hard time trying to figure out what you are trying to do. maybe reword the question?

for ($i=251; $i50; $i++)
{ 
   echo "$i\n";
}

is that what you are looking for ?


----------



## Kreij (Jun 12, 2012)

I think he wants to do a logical OR

while ($variable < 251 || $variable > 350)
{
do something;
}
endwhile;

I could be wrong. :/


----------



## digibucc (Jun 12, 2012)

use switch not while.


----------



## Nordic (Jun 12, 2012)

Thank you all for trying to help. Its been quite a while since I took any coding/php lessons, and I wasn't that great to begin with.

Thank you kreij, that code got me a bit further.
I will try to clear up what I am trying to accomplish.


```
<?php
$variable = 250; //I have been using 50, 150, and 250 to test.

$file0 = file_get_contents('./file0.txt', true);
$file1 = file_get_contents('./file1.txt', true);
$file2 = file_get_contents('./file2.txt', true);

while ($variable <= 100) //puts file0.txt content onto the page as it should
{
    echo $file0;
}
while ($variable > 101 || $variable <= 200) //puts file1.txt content onto the page as it should
{ 
	echo $file1;
}
while ($variable > 201 || $variable <= 300) //puts file1.txt content onto the page, it should put file2.txt.
{ 
	echo $file2;
}
endwhile; /Causes error. Parse error: syntax error, unexpected T_ENDWHILE in ...
?>
```

file0.txt, file1.txt, and file 2.txt all have their names for content. I triple checked. So when the variable is 250 it puts file1 onto the page, but should be putting file2 onto the page.


----------



## Disparia (Jun 12, 2012)

Setup of a while()

```
// Way #1
while ( evaluation ):
	#do something
endwhile;

// Way #2
while ( evaluation ) {
	#do something
}
```

But it doesn't seem like you need a while() for this. An if/elseif or switch, like digibucc mentioned is better. And if you're only going to use the contents of one of those files at a time, don't load them all in:

```
switch ( $variable ) {
	case 50:
		$fn = 'file0.txt';
	break;
	case 150:
		$fn = 'file1.txt';
	break;
	case 250:
		$fn = 'file2.txt';
	break;

}

echo file_get_contents($fn);
```


Depending on what you're trying to accomplish, it may be as easy as:

```
echo file_get_contents('file'.$variable.'.txt');
```
(change the file names to match the variable or change the variables to match the file names)


----------



## Nordic (Jun 12, 2012)

Jizzler said:


> Setup of a while()
> 
> ```
> // Way #1
> ...


The variable will be changing a lot, and always going up. This is only a sample of the code I am having problems with. I am looking to see whether the variable is between two numbers. Does that case funtion do that?

Like

```
// Way #1
while ( evaluatate if between x and y ):
	#insert content of file
endwhile;

// Way #2
while ( evaluation ) {
	#do something
}
```


----------



## Kreij (Jun 13, 2012)

You need an endwhile for each while. But you only need one.


```
bool AreWeHappy = false;

while (!AreWeHappy) // we're not happy yet.
{
   if (x < somevalue) do whatever;
   else if (x > somevalue) do whatever;
   else AreWeHappy = true; // condition met, we are now happy
}
endwhile;
```

Adjust for correct syntax, I'm not good at php


----------



## Nordic (Jun 13, 2012)

Kreij said:


> You need an endwhile for each while. But you only need one.
> 
> 
> ```
> ...



Are you saying like this?


```
<?php
$variable = 250;

$file0 = file_get_contents('./file0.txt', true);
$file1 = file_get_contents('./file1.txt', true);
$file2 = file_get_contents('./file2.txt', true);

while ($variable <= 100) 
{
    echo $file0;
}
endwhile;
while ($variable > 101 || $variable <= 200)
{ 
	echo $file1;
}
endwhile;
while ($variable > 201 || $variable <= 300)
{ 
	echo $file2;
}
endwhile;
?>
```


----------



## Kreij (Jun 13, 2012)

No, you don't need three while loops, you just need one that drops out when the condition is met.
Use if statements in the while loop. If php has a break statement to kill the loop you could do something like ...

While (true)
{

if ($variable <= 100) echo $file0;
else if ($variable >= 101 && $variable <= 200) echo $file1;
else if ($variable >= 201 && $variable <= 300) echo $file2;
else break;

}
endwhile;

In the case you are presenting you need logical AND (&&) not OR (||).


----------



## Nordic (Jun 13, 2012)

Kreij said:


> No, you don't need three while loops, you just need one that drops out when the condition is met.
> Use if statements in the while loop. If php has a break statement to kill the loop you could do something like ...
> 
> While (true)
> ...



This looks perfect to me. Can you explain why I would choose "and" or "or." It still doesn't like that endwhile


----------



## Disparia (Jun 13, 2012)

No endwhile if you use curly brackets.



> // Way #1
> while ( evaluation )*:*
> #do something
> *endwhile;*
> ...



Nearly all of the time you'll see the bracket structure used when looking at PHP code (less typing). But it does have an alternative structure for _if, while, for, foreach, and switch_ if you wish to use it.

http://www.php.net/manual/en/control-structures.alternative-syntax.php


As for "&&" vs "||", let's set your $variable to 210. What happens? 210 is greater than 101 OR less than 200. This is true and you'll echo $file1 for all variables greater than 100. But is it not greater than 101 AND less than 200.


----------



## Aquinus (Jun 13, 2012)

james888 said:


> Can you explain why I would choose "and" or "or." It still doesn't like that endwhile



Because the syntax is incorrect. I recommend reading up on what each control structure is used for before just using them. I was a little horrified when I saw the first post. I know developers when they do a code review to strip out all comments and read just the cost, because good code should be readable, so consider that when you write code. Also I noticed you referred to "while" as a function which is not true, it is a control structure built into PHP.

Let me try and clean it up a bit.


```
<?php
$variable = 250;
$file0 = file_get_contents('./file0.txt', true); 
$file1 = file_get_contents('./file1.txt', true); 
$file2 = file_get_contents('./file2.txt', true); 

while ($variable < 300) {
    if ($variable <= 100) {
        echo $file0;
    } else if ($variable >= 101 && $variable <= 200) {
        echo $file1;
    } else {
        echo $file2;
    }
}
```

Will "variable" always be 250 because then you can assume what the result is going to be so all this extra logic isn't needed.

Could you explain why you're trying to do with the program, not how you're trying to program it? Sometimes understanding the problem will help you design and write the code to do it because I'm blindly editing code without really knowing what you're trying to accomplish.

I also recommend print_r and var_dump for debugging and `php -l scriptname.php` to check the syntax. I actually have a bash script that checks all .php files in a directory recursively, but you need to be running *nix with the PHP cli tool.


```
#!/bin/bash
find . | grep "\(php\|html\)$" | xargs -ifoo php -l foo
```


----------

