This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Introduction to PHP Forum

View Course » View Exercise

294 points
508dfc9df2d6800200006da7_672213073
Submitted by
th3m3s
almost 3 years ago

Why are negative numbers not accepted?

Why isn't this working with negative numbers? Does PHP only know unsigned variables, where all Bits are used for positive numbers?

<?php
    $myName = "Cyrus";
    $myAge = -1;
?>

Error: Did you create a variable called $myAge and set it equal to your age as a number? Don't forget your semicolon!


2 votes

permalink

PHP does work with negative numbers. Just that this exercise is looking for a positive number. Try setting $myAge = 1;, and it should work. And according to Php.net, "PHP does not support unsigned integers."

299 points
Cf67d55abd75859eb419ca304be2f87d?s=140&d=retro
Submitted by
Garrett
almost 3 years ago

1 Comment

508dfc9df2d6800200006da7_672213073 th3m3s almost 3 years ago

Thanks for your answer Garrett. Nice to know that there are no unsigned data types in PHP. Still IMO that the code does not accept negative integers is a bug, especially with that kind of error message. If the Eric Weinstein has intended that the user shall enter only positive numbers, he should have spared this for the lecture with conditional structures and accepted negative numbers in this lecture while giving a hint that the student will learn in a later lecture how to prevent this.


0 votes

permalink

I'm not sure why you would say this isn't working. This is perfectly legitimate code (I ran it on my machine to be sure). Did you get an error? Add the line

echo $myage;

to your code and it will show "-1" (no quotes).

1 point
11408d186780aa0f93b420579b42c779?s=140&d=retro
Submitted by
jobowo
almost 3 years ago