• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Php - Simple php help please

Joined
Jun 27, 2011
Messages
6,684 (1.43/day)
Processor 7800x3d
Motherboard Gigabyte B650 Auros Elite AX
Cooling Custom Water
Memory GSKILL 2x16gb 6000mhz Cas 30 with custom timings
Video Card(s) MSI RX 6750 XT MECH 2X 12G OC
Storage Adata SX8200 1tb with Windows, Samsung 990 Pro 2tb with games
Display(s) HP Omen 27q QHD 165hz
Case ThermalTake P3
Power Supply SuperFlower Leadex Titanium
Software Windows 11 64 Bit
Benchmark Scores CB23: 1811 / 19424 CB24: 1136 / 7687
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:
<?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;
?>
 
Last edited:
Joined
Nov 2, 2010
Messages
166 (0.03/day)
Location
Philippines! :D
System Name Noobie XTXx || Portalet Crap Machine
Processor i5 2500K @ 4.5, 1.345V || i7 2640QM
Motherboard ASUS P8P67 PRO || Dell
Cooling CM Hyper 212+ || Stock
Memory 2 x 2gb GSkill DDR3 9-9-9-24 || 8GB DDR3
Video Card(s) PowerColor 6970PCS @stock || Optimus (525m)
Storage WD Caviar Blue 500 + 320 Hitachi || 500GB
Display(s) LG W2253TQ || Crappy 1366*768 15"
Case CM 690 PURE Black
Audio Device(s) On Board
Power Supply ThermalTake Toughpower XT 775w
Software Win 7 64bit
Joined
Jun 27, 2011
Messages
6,684 (1.43/day)
Processor 7800x3d
Motherboard Gigabyte B650 Auros Elite AX
Cooling Custom Water
Memory GSKILL 2x16gb 6000mhz Cas 30 with custom timings
Video Card(s) MSI RX 6750 XT MECH 2X 12G OC
Storage Adata SX8200 1tb with Windows, Samsung 990 Pro 2tb with games
Display(s) HP Omen 27q QHD 165hz
Case ThermalTake P3
Power Supply SuperFlower Leadex Titanium
Software Windows 11 64 Bit
Benchmark Scores CB23: 1811 / 19424 CB24: 1136 / 7687
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
 
Last edited:
Joined
Nov 2, 2010
Messages
166 (0.03/day)
Location
Philippines! :D
System Name Noobie XTXx || Portalet Crap Machine
Processor i5 2500K @ 4.5, 1.345V || i7 2640QM
Motherboard ASUS P8P67 PRO || Dell
Cooling CM Hyper 212+ || Stock
Memory 2 x 2gb GSkill DDR3 9-9-9-24 || 8GB DDR3
Video Card(s) PowerColor 6970PCS @stock || Optimus (525m)
Storage WD Caviar Blue 500 + 320 Hitachi || 500GB
Display(s) LG W2253TQ || Crappy 1366*768 15"
Case CM 690 PURE Black
Audio Device(s) On Board
Power Supply ThermalTake Toughpower XT 775w
Software Win 7 64bit
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

Administrator
Staff member
Joined
May 14, 2004
Messages
27,037 (3.71/day)
Processor Ryzen 7 5700X
Memory 48 GB
Video Card(s) RTX 4080
Storage 2x HDD RAID 1, 3x M.2 NVMe
Display(s) 30" 2560x1600 + 19" 1280x1024
Software Windows 10 64-bit
i'm having a hard time trying to figure out what you are trying to do. maybe reword the question?

for ($i=251; $i:love:50; $i++)
{
echo "$i\n";
}

is that what you are looking for ?
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
I think he wants to do a logical OR

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

I could be wrong. :/
 
Joined
May 21, 2009
Messages
4,966 (0.91/day)
System Name i7-PC / HTPC / iMac
Processor i7 3820 / Phenom II 940
Motherboard GIGABYTE G1.ASSASSIN2 / M3A79-T Deluxe
Cooling Corsair Hydro H100i / Scythe II (HS only)
Memory G.SKILL Trident X Series 8GB (2 x 4GB) DDR3 1600mhz / 4GB DDR2 1066 (@800) Corsair Dominator
Video Card(s) GB Radeon HD 7950s 3GB / GB Radeon HD 7950s 3GB
Storage 2x 80GB Intel X-25, 2x600gb SATA, 1x1tb 5400RPM storage /1x600GB, 3x500GB,1x160,1x120 SATA
Display(s) 1x 27" Yamakasi / Vizio 42" HDTV
Case Lian Li Lancool PC-K58 / Antec 900
Audio Device(s) HT Omega Striker 7.1 / Onboard and HDMI from ATi Card
Power Supply PC Power & Cooling 750W / 610W
Software Ubuntu / Windows 8.1 Pro / OS X / PHPStorm / Gaming
Joined
Jun 27, 2011
Messages
6,684 (1.43/day)
Processor 7800x3d
Motherboard Gigabyte B650 Auros Elite AX
Cooling Custom Water
Memory GSKILL 2x16gb 6000mhz Cas 30 with custom timings
Video Card(s) MSI RX 6750 XT MECH 2X 12G OC
Storage Adata SX8200 1tb with Windows, Samsung 990 Pro 2tb with games
Display(s) HP Omen 27q QHD 165hz
Case ThermalTake P3
Power Supply SuperFlower Leadex Titanium
Software Windows 11 64 Bit
Benchmark Scores CB23: 1811 / 19424 CB24: 1136 / 7687
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:
<?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.
 
Last edited:
Joined
Aug 10, 2007
Messages
4,267 (0.70/day)
Location
Sanford, FL, USA
Processor Intel i5-6600
Motherboard ASRock H170M-ITX
Cooling Cooler Master Geminii S524
Memory G.Skill DDR4-2133 16GB (8GB x 2)
Video Card(s) Gigabyte R9-380X 4GB
Storage Samsung 950 EVO 250GB (mSATA)
Display(s) LG 29UM69G-B 2560x1080 IPS
Case Lian Li PC-Q25
Audio Device(s) Realtek ALC892
Power Supply Seasonic SS-460FL2
Mouse Logitech G700s
Keyboard Logitech G110
Software Windows 10 Pro
Setup of a while()
PHP:
// 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:
PHP:
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:
PHP:
echo file_get_contents('file'.$variable.'.txt');
(change the file names to match the variable or change the variables to match the file names)
 
Joined
Jun 27, 2011
Messages
6,684 (1.43/day)
Processor 7800x3d
Motherboard Gigabyte B650 Auros Elite AX
Cooling Custom Water
Memory GSKILL 2x16gb 6000mhz Cas 30 with custom timings
Video Card(s) MSI RX 6750 XT MECH 2X 12G OC
Storage Adata SX8200 1tb with Windows, Samsung 990 Pro 2tb with games
Display(s) HP Omen 27q QHD 165hz
Case ThermalTake P3
Power Supply SuperFlower Leadex Titanium
Software Windows 11 64 Bit
Benchmark Scores CB23: 1811 / 19424 CB24: 1136 / 7687
Setup of a while()
PHP:
// 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:
PHP:
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:
PHP:
echo file_get_contents('file'.$variable.'.txt');
(change the file names to match the variable or change the variables to match the file names)
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
PHP:
// Way #1
while ( evaluatate if between x and y ):
	#insert content of file
endwhile;

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

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
You need an endwhile for each while. But you only need one.

Code:
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
 
Joined
Jun 27, 2011
Messages
6,684 (1.43/day)
Processor 7800x3d
Motherboard Gigabyte B650 Auros Elite AX
Cooling Custom Water
Memory GSKILL 2x16gb 6000mhz Cas 30 with custom timings
Video Card(s) MSI RX 6750 XT MECH 2X 12G OC
Storage Adata SX8200 1tb with Windows, Samsung 990 Pro 2tb with games
Display(s) HP Omen 27q QHD 165hz
Case ThermalTake P3
Power Supply SuperFlower Leadex Titanium
Software Windows 11 64 Bit
Benchmark Scores CB23: 1811 / 19424 CB24: 1136 / 7687
You need an endwhile for each while. But you only need one.

Code:
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

Are you saying like this?

PHP:
<?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

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
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 (||).
 
Last edited:
Joined
Jun 27, 2011
Messages
6,684 (1.43/day)
Processor 7800x3d
Motherboard Gigabyte B650 Auros Elite AX
Cooling Custom Water
Memory GSKILL 2x16gb 6000mhz Cas 30 with custom timings
Video Card(s) MSI RX 6750 XT MECH 2X 12G OC
Storage Adata SX8200 1tb with Windows, Samsung 990 Pro 2tb with games
Display(s) HP Omen 27q QHD 165hz
Case ThermalTake P3
Power Supply SuperFlower Leadex Titanium
Software Windows 11 64 Bit
Benchmark Scores CB23: 1811 / 19424 CB24: 1136 / 7687
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 (||).

This looks perfect to me. Can you explain why I would choose "and" or "or." It still doesn't like that endwhile
 
Joined
Aug 10, 2007
Messages
4,267 (0.70/day)
Location
Sanford, FL, USA
Processor Intel i5-6600
Motherboard ASRock H170M-ITX
Cooling Cooler Master Geminii S524
Memory G.Skill DDR4-2133 16GB (8GB x 2)
Video Card(s) Gigabyte R9-380X 4GB
Storage Samsung 950 EVO 250GB (mSATA)
Display(s) LG 29UM69G-B 2560x1080 IPS
Case Lian Li PC-Q25
Audio Device(s) Realtek ALC892
Power Supply Seasonic SS-460FL2
Mouse Logitech G700s
Keyboard Logitech G110
Software Windows 10 Pro
No endwhile if you use curly brackets.

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

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

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

Resident Wat-man
Joined
Jan 28, 2012
Messages
13,147 (2.94/day)
Location
Concord, NH, USA
System Name Apollo
Processor Intel Core i9 9880H
Motherboard Some proprietary Apple thing.
Memory 64GB DDR4-2667
Video Card(s) AMD Radeon Pro 5600M, 8GB HBM2
Storage 1TB Apple NVMe, 4TB External
Display(s) Laptop @ 3072x1920 + 2x LG 5k Ultrafine TB3 displays
Case MacBook Pro (16", 2019)
Audio Device(s) AirPods Pro, Sennheiser HD 380s w/ FIIO Alpen 2, or Logitech 2.1 Speakers
Power Supply 96w Power Adapter
Mouse Logitech MX Master 3
Keyboard Logitech G915, GL Clicky
Software MacOS 12.1
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:
<?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.

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