Jump to content

PHP help with $values

bomberblyat
Go to solution Solved by bomberblyat,
15 hours ago, leodaniel said:

Always whats more human (you and me :P) readable.

Name your variables appropriately (having var1-var15 is bad, its hard to understand what they each represent), always step by step so that it's easy to understand.

Just try to make it simple. Imagine you would have to look at your script again in 10 years... how long would you take to understand it (goal is to shorten this time). I think it's important to always code with that in mind. 

 

In the 2. code example it's really hard to understand what the code does, what the variables are. It takes quit some time to understand what it does.

 

i fixed it the code works it was pain in the ass, but it works xD

Hi

 

is it possible anyhow to make something like this   ?

  echo $value / $value1 = $value3

basically use php divide output as new value.

 

I appreciate any answers :)

Link to comment
Share on other sites

Link to post
Share on other sites

17 minutes ago, bomberblyat said:

Hi

 

is it possible anyhow to make something like this   ?


  echo $value / $value1 = $value3

basically use php divide output as new value.

 

I appreciate any answers :)

4
$value1 = 100;
$value3 = 10;
$value = $value1 / $value3;
echo $value;

 

Make sure to quote me or use @PorkishPig to notify me that you replied!

 

 

Desktop

CPU - Ryzen 9 3900X | Cooler - Noctua NH-D15 | Motherboard - ASUS TUF X570-PLUS RAM - Corsair Vengeance LPX DDR4-3200 32GB Case - Meshify C

GPU - RTX 3080 FE PSU - Straight Power 11 850W Platinum Storage - 980 PRO 1TB, 960 EVO 500GB, S31 1TB, MX500 500GB | OS - Windows 11 Pro

 

Homelab

CPU - Core i5-11400 | Cooler - Noctua NH-U12S | Motherboard - ASRock Z590M-ITX RAM - G.Skill Ripjaws V DDR4-3600 32GB (2x16)  | Case - Node 304

PSU - EVGA B3 650W | Storage - 860 EVO 256GB, Sabrent Rocket 4.0 1TB, WD Red 4TB (x6 in RAIDZ1 w/ LSI 9207-8i) | OS - TrueNAS Scale (Debian)

Link to comment
Share on other sites

Link to post
Share on other sites

If you're just trying to cut down on lines it can be done with

 

$value1 = 100;
$value3 = 10;

echo $value = ($value1 / $value3);
Output -> 10

//Showing that it works to print out the $value
echo $value;
Output -> 10

 

| Intel i5 4670k @ 4.3GHz | XFX RX 480 8 GB | Asus Z87i-Pro | 8.0 GB Kinston DDR3 | Samsung Evo 120GB SSD |

Link to comment
Share on other sites

Link to post
Share on other sites

<html>
<body>
<?php     $value1 = 500;
			$value2 = 250;
			$value3 = 100;
			$value5 = 0;
			$value = $_POST["name"];
		  if ($value > $value1)
		$value6 = $value / $value1;
		$value7 = $value6 * $value1;
		$uus = $value - $value7;

				  if ($uus > $value2)
		$value10 = $uus / $value2;
		$value11 = $value10 * $value2;
		$uus2 = $value - $value11;

				  if ($uus2 > $value3)
		$value13 = $uus2 / $value3;
		$value14 = $value13 * $value3;
		$uus3 = $value - $value14;
		echo  "       $value6";
		echo  "       $value10";
		echo  "       $value13";


		  
		  

	
?>
</body>
</html>

My FRIEND DID THIS CODE IT DOESNT MAKE ANY SENSE RIGHT?

Link to comment
Share on other sites

Link to post
Share on other sites

So yes this is possible:

<?php

$v1 = 10;
$v2 = 5;

echo $result = ($v1 / $v2); 
// output: 2

var_dump($result);
// output: int(2)

The question is more, should you write this?

I would argue for no. I think it's more readable to first only create the value $result and then echo it separately. I think it's more readable and in the end this should be your goal. The computer will always understand messy code, but humans tend to have more problem with that. Think of it like this, you should alway write code for humans first, not for computers. If you stumble across your code in a year, which would you find easier to understand (or anyone else)?

 

I would go with that. Even if it's one line longer ;)

<?php

$var1 = 10;
$var2 = 5;

$result = ( $var1 / $var2 );

echo $result;

 

Business Management Student @ University St. Gallen (Switzerland)

HomeServer: i7 4930k - GTX 1070ti - ASUS Rampage IV Gene - 32Gb Ram

Laptop: MacBook Pro Retina 15" 2018

Operating Systems (Virtualised using VMware): Windows Pro 10, Cent OS 7

Occupation: Software Engineer

Link to comment
Share on other sites

Link to post
Share on other sites

44 minutes ago, leodaniel said:

So yes this is possible:


<?php

$v1 = 10;
$v2 = 5;

echo $result = ($v1 / $v2); 
// output: 2

var_dump($result);
// output: int(2)

The question is more, should you write this?

I would argue for no. I think it's more readable to first only create the value $result and then echo it separately. I think it's more readable and in the end this should be your goal. The computer will always understand messy code, but humans tend to have more problem with that. Think of it like this, you should alway write code for humans first, not for computers. If you stumble across your code in a year, which would you find easier to understand (or anyone else)?

 

I would go with that. Even if it's one line longer ;)


<?php

$var1 = 10;
$var2 = 5;

$result = ( $var1 / $var2 );

echo $result;

 

my head is about to explode i have no idea at all what i should do now please help xD

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, bomberblyat said:

my head is about to explode i have no idea at all what i should do now please help xD

Always whats more human (you and me :P) readable.

Name your variables appropriately (having var1-var15 is bad, its hard to understand what they each represent), always step by step so that it's easy to understand.

Just try to make it simple. Imagine you would have to look at your script again in 10 years... how long would you take to understand it (goal is to shorten this time). I think it's important to always code with that in mind. 

 

In the 2. code example it's really hard to understand what the code does, what the variables are. It takes quit some time to understand what it does.

 

Business Management Student @ University St. Gallen (Switzerland)

HomeServer: i7 4930k - GTX 1070ti - ASUS Rampage IV Gene - 32Gb Ram

Laptop: MacBook Pro Retina 15" 2018

Operating Systems (Virtualised using VMware): Windows Pro 10, Cent OS 7

Occupation: Software Engineer

Link to comment
Share on other sites

Link to post
Share on other sites

15 hours ago, leodaniel said:

Always whats more human (you and me :P) readable.

Name your variables appropriately (having var1-var15 is bad, its hard to understand what they each represent), always step by step so that it's easy to understand.

Just try to make it simple. Imagine you would have to look at your script again in 10 years... how long would you take to understand it (goal is to shorten this time). I think it's important to always code with that in mind. 

 

In the 2. code example it's really hard to understand what the code does, what the variables are. It takes quit some time to understand what it does.

 

i fixed it the code works it was pain in the ass, but it works xD

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×