I'm trying to check if these 3 numbers are odd:
if ( ($pezzi % 1) && ($base % 1) && ($altezza % 1) ) {
}
Why my script don't work? I do not understand why..
if ( ($pezzi % 2 !=0) and ($base % 2 !=0) and ($altezza % 2 !=0) ) {
}
Try this way:
if((number1 % 2) != 0 AND (number2 % 2) != 0 AND (number3 % 2) != 0){
-- do something
}
You should do like this:
if ($pezzi%2!=0 && $base%2!=0 && $altezza%2!=0) {
}
if ( ($pezzi & 1) && ($base & 1) && ($altezza & 1) ) {
}
Your statement is true. You just didn't compare it with 0:
if(($pezzi % 2) != 0 && ($base % 2) != 0 && ($altezza % 2) != 0){
}
Related
In the project I am working, I get an age like
1Year 2Months 3Weeks 4Days
I am trying to do a comparison like
If age is less than 8 weeks, then it is a baby
If age less than or equal to 12 months/1 year then it is young
If greater than 1 year, it is adult. The code which I try is given below. It is confusing.
Is there a way to simplify this using some in built functions?
function getAgeGroup($age_years, $age_months, $age_weeks, $age_days) {
$age_group = 'baby';
if (!$age_years && !$age_months && (!$age_weeks || ($age_weeks <= 8 && (!$age_days || $age_days < 1)))) {
$age_group = 'baby';
}
elseif (!$age_years && ($age_months == 1 || $age_months == 2) && (!$age_weeks && !$age_days)) {
$age_group = 'baby';
}
elseif (!$age_years && $age_months == 1 && $age_weeks < 4 && $age_days) {
$age_group = 'young';
}
elseif (!$age_years && $age_months == 1 && $age_weeks > 4) {
$age_group = 'young';
}
elseif (!$age_years && ($age_months >= 2 && $age_months < 12) && ($age_weeks > 0 || $age_days > 0)) {
$age_group = 'young';
}
elseif (!$age_years && ($age_months > 0 && $age_months <= 12) && ($age_weeks >= 8 && $age_days > 1)) {
$age_group = 'young';
}
elseif (!$age_years && ($age_months > 2 && $age_months <= 12) && (!$age_weeks && !$age_days)) {
$age_group = 'young';
}
elseif ($age_years == 1 && !$age_months && !$age_weeks && !$age_days) {
$age_group = 'young';
}
elseif (($age_years > 0 || $age_months >= 12) && ($age_weeks > 0 || $age_days > 0)) {
$age_group = 'adult';
}
elseif (($age_years > 0 || $age_months > 0)) {
$age_group = 'adult';
}
elseif ($age_years > 1) {
$age_group = 'adult';
}
return $age_group;
}
you can try to something like this: eval.in to get use of the DateTime and DateInterval classes.
I cannot find an answer to this and I am sure it is right in front of me. How do I say in PHP as part of an IF statement the following:
if NOT ( (variable1 == 10) && (variable2 == 20) )
Thanks!
You can use ! to achieve this
if ( (variable1 != 10) || (variable2 != 20) )
Or simply
if (!((variable1 == 10) && (variable2 == 20)))
if NOT ( (variable1 == 10) && (variable2 == 20) )
==>
if (! ( (variable1 == 10) && (variable2 == 20) ) )
<==>
if ( (variable1 != 10) ||(variable2 != 20) )
! mean not
so U can use it like this
if (! ( ($variable1 == 10) && ($variable2 == 20) ) )
or
if ( ($variable1 != 10) ||($variable2 != 20) )
&& will be || because !&& = ||
and U can't use variable1 without $ if U don't define it like this
define('variable1','value of variable1');
but then U can't change it's value so if statement will always have the same result and for that U should use $variable1
In my database I have "maximum files" but I would like to set it at 0 for unlimited.
Therefore I need a way to
if ($count >= $max){
//a value of 0 in $max should not be here
} else {
//but here
}
Is this possible or do I have to create an exclusion for 0?
if($max && ($count >= $max)) ....
if (($max !== 0) && ($count >= $max) ){
if (!empty($max) && $count >= $max ) { ...
I have long IF:
if(rand(1, 100) == 22 && $smth < time() &&
$smths > 5 && $sxsxsx > 250 &&
!$_SESSION['false'])
{
echo "wow, big if just happened!";
}
How to write it more "prettier"?
I prefer breaking before the boolean operators.
if(rand(1, 100) == 22
&& $smth < time()
&& $smths > 5
&& $sxsxsx > 250
&& !$_SESSION['false']
)
I like to name my conditions and group them so its clear what their purpose is.
$is22 = rand(1, 100) == 22;
$someTime = $smth < time() && $smths > 5;
$meetsSx = $sxsxsx > 250;
$inSession = !$_SESSION['false'];
if ($is22 && $someTime && $meetsSx && $inSession) {
// do something
}
$isSomethingValid = rand(1, 100) == 22
&& $smth < time()
&& $smths > 5
&& $sxsxsx > 250
&& !$_SESSION['false'];
if ($isSometingValid) {
// do something
}
In accordance with my answer to the related
Multiple condition IF statement
this should be refactored with Decompose Conditional, which means you should make the individual tests into separate functions. And you should get rid of the magic numbers and meaningless variable names. I would give you an example on how to do that for your code, but the code is incomprehensible.
Always indent to the enclosing statement one extra than the body of the block. You would write a function like this:
function (reallylongparam, reallylongparam, reallylongparam,
reallylongparam, reallylongparam) {
doStuff()
}
so you'd write your if statement like this:
if(rand(1, 100) == 22 && $smth < time() && $smths > 5
&& $sxsxsx > 250 && !$_SESSION['false']) {
doStuff();
}
probably
if(
rand(1, 100) == 22 &&
$smth < time() &&
$smths > 5 &&
$sxsxsx > 250 &&
!$_SESSION['false']
) {
echo "wow, big if just happened!";
}
cheers
Making your code readable is a very important aspect when it comes to supporting your code - someone else might have to do that support.
Have a look at coding styles (search around for more info if you must).
Personally I would format that snippet like so:
if (
rand(1, 100) == 22
&&
$smth < time()
&&
$smths > 5
&&
$sxsxsx > 250
&&
!$_SESSION['false']
)
{
echo "wow, big if just happened!";
}
Just encapsulate the boolean logic in a seperate function
You could also make your variable names easier to read.
Will any one please tell me how to run this class. I am getting the FATAL ERROR: Fatal error: Call to undefined function readnumber() in E:\Program Files\xampp\htdocs\numberToWords\numberToWords.php on line 20 while giving input as 120
<?php
class Test
{
function readnumber($num, $depth)
{
$num = (int)$num;
$retval ="";
if ($num < 0) // if it's any other negative, just flip it and call again
return "negative " + readnumber(-$num, 0);
if ($num > 99) // 100 and above
{
if ($num > 999) // 1000 and higher
$retval .= readnumber($num/1000, $depth+3);
$num %= 1000; // now we just need the last three digits
if ($num > 99) // as long as the first digit is not zero
$retval .= readnumber($num/100, 2)." hundred\n";
$retval .=readnumber($num%100, 1); // our last two digits
}
else // from 0 to 99
{
$mod = floor($num / 10);
if ($mod == 0) // ones place
{
if ($num == 1) $retval.="one";
else if ($num == 2) $retval.="two";
else if ($num == 3) $retval.="three";
else if ($num == 4) $retval.="four";
else if ($num == 5) $retval.="five";
else if ($num == 6) $retval.="six";
else if ($num == 7) $retval.="seven";
else if ($num == 8) $retval.="eight";
else if ($num == 9) $retval.="nine";
}
else if ($mod == 1) // if there's a one in the ten's place
{
if ($num == 10) $retval.="ten";
else if ($num == 11) $retval.="eleven";
else if ($num == 12) $retval.="twelve";
else if ($num == 13) $retval.="thirteen";
else if ($num == 14) $retval.="fourteen";
else if ($num == 15) $retval.="fifteen";
else if ($num == 16) $retval.="sixteen";
else if ($num == 17) $retval.="seventeen";
else if ($num == 18) $retval.="eighteen";
else if ($num == 19) $retval.="nineteen";
}
else // if there's a different number in the ten's place
{
if ($mod == 2) $retval.="twenty ";
else if ($mod == 3) $retval.="thirty ";
else if ($mod == 4) $retval.="forty ";
else if ($mod == 5) $retval.="fifty ";
else if ($mod == 6) $retval.="sixty ";
else if ($mod == 7) $retval.="seventy ";
else if ($mod == 8) $retval.="eighty ";
else if ($mod == 9) $retval.="ninety ";
if (($num % 10) != 0)
{
$retval = rtrim($retval); //get rid of space at end
$retval .= "-";
}
$retval.=readnumber($num % 10, 0);
}
}
if ($num != 0)
{
if ($depth == 3)
$retval.=" thousand\n";
else if ($depth == 6)
$retval.=" million\n";
if ($depth == 9)
$retval.=" billion\n";
}
return $retval;
}
}
$objTest = new Test();
$objTest->readnumber(120,0);
?>
You are using the readnumber function in the class itself, try this instead where it appears $this->readnumber