Jump to content

php


j.silver

Recommended Posts

Dear all: below division spat an unexpected result. The first line of code makes $var7 equals 50; this later value is then divided by 7. Strangely, the output was 10. How come?

 

<?php $var7 = 10; ?>
Multiplication: <?php echo $var7 * 5; ?><br />
Division: <?php $var7/7; echo $var7; ?><br />
Link to comment
Share on other sites

Variables don't change their value when you operate them. Take this example:

$a = 1;$b = 2;$c = $a + $b;echo $a; // Shows "1"echo $b; // Shows "2"echo $c; // Shows "3"
Link to comment
Share on other sites

It seems that the first part of the division is wrong $var7/7 (should be $var7 /= 7;), so it ignored it and just spat the same original value of $var7. But why $var7/7 has not resulted in an error message?

Link to comment
Share on other sites

Using $var7/7 you are just requiring the varible that hold the value of 10 to be divided by 7, it will do that, and there will be no error as its perfectly valid, as it not changing the variable value but using it to make a calculation, by using echo $var7 will show just show 10 again, while echo $var7/7 as used for the multiplication example would give you the correct result.Changing the actual varible value and showing the change takes this action$var7 = $var7/7;echo $var7;

Link to comment
Share on other sites

You need to follow example on the tutorial not just testing what you feel, it's a time consuming. php is a template, you would not be able to add anything to it.

 

I don't understand what you're trying to say. PHP is a programming language, not a "template".

 

Just copying code from the tutorial is a good way to not learn. Doing experiments helps you understand how PHP works.

Link to comment
Share on other sites

I fully agree with Ingolme. I have covered advanced level of PHP topics, but what I had been doing was understanding the tutorial/code but not playing with it to see how exactly the code works, relying only on getting ready-made codes and tailoring them to my need. Then came the problem, that I found myself not able to modify the code as I wanted to or getting unexpected results that I could not manage to fix. Now, I have decided to go back to the basics, play with the code to get in-depth knowledge of how exactly PHP handles the code.

Link to comment
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
×
×
  • Create New...