Even or Odd String Check - php

I'm trying to write a program that checks whether the input string is even or odd and I keep getting errors I can't figure out why.
(i = odd, ii = even, iii = odd, iiii = even) and so on.
I try
<?php
//checking even and odd
echo '<form action="" method="post">';
echo "<input type='text' name='val'>\n";
echo "<button type='submit' name='submit'>Check</button>\n";
echo "</form>";
$val = 0;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["val"])) {
$Err = "<span style ='color: red;'>Required.</span>";
echo $Err;
die();
} else {
$val = $_POST["val"];
}
$even = ($val % 2 == 0);
$odd = ($val % 2 != 0);
if ($val > 0){
if($even){
echo "Sting is even.";
} else {
echo "Sting is odd.";
}
} else {
echo "Error";
}
}
?>

You can't divide a string, you need to check the length. Based on your examples of i, ii, iii, iiii etc:
if(strlen($val) % 2 == 0) {
//even
} else {
//odd
}

Solution
<?php
$val = "10";
if (is_numeric($val)){
if($val % 2 == 0){
echo "Even";
}
else{
echo "Odd";
}
}
else {
echo "Invalid number";
}
?>
Here, is_numeric($val) checks if the input is a valid number and not a string. This will avoid Warning: A non-numeric value encountered
You should further handle the cases where the input is a negative number or a floating-point number.

Related

Loop from array not executed

I am working on an android app that should create some records on a MySQL database.
This is the PHP file that receives the POST values from the app.
As you may see, there are three arrays that collect specific POST.
The first and second loops are working fine and executing the related guardar_post_media() function.
But the third loop is not executed and there are real values on the variables inside the third array.
May be there is something wrong that I can´t detect, may be you can.
<?php
require_once '../mis_php_functions/funciones_basicas.php';
if($_SERVER['REQUEST_METHOD']=='POST'){
$val39 = $_POST['val39'];
$val40 = $_POST['val40'];
$val46 = $_POST['val46'];
$val48 = $_POST['val48'];
$val50 = $_POST['val50'];
$val52 = $_POST['val52'];
$val54 = $_POST['val54'];
$val56 = $_POST['val56'];
$val58 = $_POST['val58'];
$val60 = $_POST['val60'];
$val62 = $_POST['val62'];
$val64 = $_POST['val64'];
$val65 = $_POST['val65'];
$val67 = $_POST['val67'];
$val69 = $_POST['val69'];
$val71 = $_POST['val71'];
$val73 = $_POST['val73'];
$val75 = $_POST['val75'];
$val77 = $_POST['val77'];
$val79 = $_POST['val79'];
$val81 = $_POST['val81'];
$val82 = $_POST['val82'];
$val83 = $_POST['val83'];
$val84 = $_POST['val84'];
$val85 = $_POST['val85'];
$val86 = $_POST['val86'];
$val87 = $_POST['val87'];
$val88 = $_POST['val88'];
$val89 = $_POST['val89'];
$val100 = $_POST['val100'];
$val101 = $_POST['val101'];
$val102 = $_POST['val102'];
$val103 = $_POST['val103'];
$val104 = $_POST['val104'];
$status = 1;
$post = guardar_post($val40,$val39,$val100,$val102,$status,$val103);
if ($post != false) {
$fotos = array($val48,$val50,$val52,$val54,$val56,$val58,$val60,$val62,$val64);
$arrayLength = count($fotos);
echo "Numero de fotos ".$arrayLength;
$i = 0;
while ($i < $arrayLength)
{
if ($fotos[$i] == 0){
}
else{
guardar_post_media(1,$fotos[$i],$val102,$val100,$post);
}
echo "<br />".$fotos[$i] ."<br />";
$i++;
}
$videos = array($val67,$val69,$val71,$val73,$val75,$val77,$val79,$val81,$val83);
$arrayLength2 = count($videos);
echo "Numero de videos ".$arrayLength2;
$i = 0;
while ($i < $arrayLength2)
{
if ($videos[$i] == 0){
}
else{
guardar_post_media(2,$videos[$i],$val102,$val100,$post);
}
echo "<br />".$videos[$i] ."<br />";
$i++;
}
$youtube = array($val85,$val86,$val87,$val88,$val89);
$arrayLength3 = count($youtube);
echo "Numero de youtube ".$arrayLength3;
$i = 0;
while ($i < $arrayLength3)
{
if ($youtube[$i] == 0){
}
else{
guardar_post_media(3,$youtube[$i],$val102,$val100,$post);
}
echo "<br />".$youtube[$i] ."<br />";
$i++;
}
sendMessageNuevoPost($val39,$val102,$val103,$val104); // envio de push
}
else{
echo 'error';
}
}
?>
You have:
if ($youtube[$i] == 0){
}
else {
}
But POST vars are all strings. Change to:
if ($youtube[$i] == "0"){
}
else {
}
In otherwords, an equality on a string to numerical 0 will be true in your cases. And thus your else never executes.
*** Edit. PROOF
$test1 = "filename.dat";
$test2 = "2939";
$test3 = "some useful data";
$test4 = "0";
if ($test1 == 0) {
// Dont do anything
}
else {
echo "Do Work 1.";
}
if ($test2 == 0) {
// Dont do anything
}
else {
echo "Do Work 2.";
}
if ($test3 == 0) {
// Dont do anything
}
else {
echo "Do Work 3.";
}
if ($test4 == 0) {
// Dont do anything
}
else {
echo "Do Work 4.";
}
Only echoes out Do Work 2.. All other echo()s are not run because the equality of a string to a number 0 will return true except in those cases where the string also is numerical, and then the interpreter will compare the numerical values.
As how this relates to the OP's question: It can be inferred that the OP's POST vars must contain some non-numerical data because the OP insists that the 3rd array is populated:
But the third loop is not executed and there are real values on the variables inside the third array.
I will add likely the loop is executed, but not giving the expected the results due the reasons so mentioned.
Tested on PHP 5 and 7.

PHP empty variable inside condition

I have a condition in which it is using a variable to pull either a number through 0-17, the string "MAKEUP", or the variable will be empty. I would like it to output the text "WIN" if the variable is greater than the number 8 and "LOSS" if the variable is less than the number 9. I would also like it to out "MAKEUP" if the variable consist of the string MAKEUP, and to display nothing if the variable is empty. Seems pretty simple to me, but I'm having issues particularly with the empty part. Can anyone let me know what I am doing wrong here? Code below
<?php
$t1w8 = '';
$result = $t1w8;
if ($result > 8 && !empty($result)) {
echo 'WON';
} elseif ($result < 9 && !empty($result)) {
echo 'LOSS';
} elseif ($result == 'MAKEUP') {
echo '-';
} else {
echo 'yooo';
}
?>
Make some changes in your conditions like this
<?php
//$result = "MAKEUP";
$result = 0;
if ($result === 'MAKEUP') {
echo '-';
}else if (is_numeric($result) && $result < 9 ) {
echo 'LOSS';
}else if (is_numeric($result) && $result >= 9 ) {
echo 'WON';
} else{
echo 'yooo';
}
?>
Live demo : https://eval.in/897120
try with this code
<?php
//$result = "MAKEUP";
$result = "";
//$result = "9";
//$result = "-";
if ($result == 'MAKEUP' && !empty($result) ) {
echo '-';
} elseif ($result > 8 && !empty($result)) {
echo 'WON';
} elseif ($result <= 8 && !empty($result)) {
echo 'LOSS';
} else {
echo 'yooo';
}
?>
for demo :demo code here
You have explained that your number range is from 0-17.
You have also explained that you could get the word MAKEUP.
Based upon those constraints we could use something like this
$output = "";
// Do we have something?
if(strlen($result) > 0) {
if (strtolower($result) == "makeup") {
$output = "MAKEUP";
}
// assumes a single digit string
else if ($result < 9) {
$output = "LOSS";
} else if ($result <= 17) {
$output = "WIN";
}
}
echo $output;

big IF function not working in PHP?

Dears,
I am surprised why the PHP 'IF ELSE' function is not working properly. I guess it's a bit long but I made Algorithm for it and logically it would work perfectly. Can anyone have any clue why the function not working properly . please ..
<?php
//entry Marks
$tc=80;$tf=33;$pc=null;$pf=50;
if($tc!=NULL && $tf!=NULL && $pc!=NULL && $pf!=NULL){
echo $tc." ".$tf." ".$pc." ".$pf;
}else{
if($tc!=NULL){
if($tf!=NULL && $pc!=NULL && $pf!=NULL){
echo "tf.pc.pf";
}else{
if($tf!=NULL){
if($pc!=NULL && $pf!=NULL){
echo "tf.pc.pf";
}else{
if($pc!=NULL){
if($pf!=NULL){
echo "tf.pc.pf";
}else{
echo "tf.pc";
}
}else{
if($pf!=NULL){
echo "tf.pf";
}else{
echo "tf";
}
}
}
}else{
if($pc!==NULL && $pf!=NULL){
echo "pc.pf";
}else{
if($pc!=NULL){
if($pf!=NULL){
echo "pc.pf";
}else{
echo "pc";
}
}else{
if($pf!=NULL){
echo "pf";
}else{
echo "null";
}
}
}
}
}
}else{ //2nd part
if($tf!=NULL && $pc!=NULL && $pf!=NULL){
echo "tf.pc.pf";
}else{
if($tf!=NULL){
if($pc!=NULL && $pf!=NULL){
echo "tf.pc.pf";
}else{
if($pc!=NULL){
if($pf!=NULL){
echo "tf.pc.pf";
}else{
echo "tf.pc";
}
}else{
echo "tf";
}
}
}else{
if($pc!=NULL && $pf!=NULL){
echo "pc.pf";
}else{
if($pc!=NULL){
if($pf!=NULL){
echo "pc.pf";
}else{
echo "pc";
}
}else{
if($pf!=NULL){
echo "pf";
}else{
echo "null";
}
}
}
}
}
}
}
?>
Your code is really difficult to follow, this code sample below shows a simplified version which appends to a string the values or text depending on if the variable is NULL or not. You can change what the if-else statements add to the result string easily. At the end I remove the "." at the end of the final string.
$result = "";
if($tc == NULL){
$result .= "tc.";
}else{
$result .= $tc.".";
}
if($tf == NULL){
$result .= "tf.";
}else{
$result .= $tf.".";
}
if($pc == NULL){
$result .= "pc.";
}else{
$result .= $pc.".";
}
if($pf == NULL){
$result .= "pf.";
}else{
$result .= $pf.".";
}
$output = rtrim($result, '.');
echo $output;
I will update my answer when its a bit clearer what output you are after
As noted in the other answer, your code is much longer than needed. This length makes it hard to follow. You could use a foreach loop to go through all your columns (which will make adding columns much easier).
Assuming I understand what you are looking for, you could use something like:
$tc=80;$tf=33;$pc=null;$pf=50;
$columns = array('tc'=>$tc,'tf'=>$tf,'pc'=>$pc,'pf'=>$pf);
$i = 0;
$output = "";
foreach ($columns as $key=> $column) {
if (!$column == null) {
$output .= $key.".";
}
}
// strip last period:
$output = rtrim($output, '.');
echo $output;
Link to php sandbox for demo here.
I do not have the answer of why your large if-statement do not work. But I do have a suggestion of making a PHP function to handle your specific need:
function check($tc=null,$tf=null,$pc=null,$pf=null){
$r = array();
if($tc != null){ $r[] = 'tc'; }
if($tf != null){ $r[] = 'tf'; }
if($pc != null){ $r[] = 'pc'; }
if($pf != null){ $r[] = 'pf'; }
if(empty($r)){
return 'null';
} else {
return implode('.', $r);// MAKE STRING OF ARRAY
}
}
And call the function like this:
$tc=80;$tf=33;$pc=null;$pf=50;
echo check($tc, $tf, $pc, $pf);
Try it out: https://eval.in/735202

Why is the return value of this PHP function zero for a long array?

When the row['error'] is bigger than 35, the value isn't present and the result of the function is 0. Where is the problem?
<?php
if ($row['error'] == "")
{
$error = "0";
}
else
{
$error = $row['error'];
}
if ($row['error'] != "")
{
if (strlen($error) > 35)
{
$error = substr($row['error'],0,32) + "...";
}
else
{
$error = $row['error'];
}
}
?>
Change
$error = substr($row['error'],0,32) + "...";
to:
$error = substr($row['error'],0,32) . "...";
The concatenate operator in PHP isn't a plus (+) sign; it's a period (.) sign
All this code is not necessary. The second condition is redundant, and it doubles the else condition from the above. Make it all with just these few lines of code:
<?php
$error = $row['error'];
if (strlen($error) > 35) {
$error = substr($row['error'],0,32) . "...";
}
?>
Because you check:
if(strlen($error) > 35) {
}

PHP echo text if variable is blank

I am trying to echo €00.00 if my variable $amount equals zero
I have done something wrong can you help??
Code
while ($row9 = mysql_fetch_array($result9))
{
$amount = $row9['amount'];
}
//$amount = $amount / 60;
//$amount = round($amount, 2);
if $amount == 0 echo "<b>Balance: €00.00</b>";
else
echo "<b>Balance: $$amount</b>";
You need to put the if/else in the loop, and you have some invalid syntax (missing parens and double $). So:
while ($row9 = mysql_fetch_array($result9))
{
$amount = $row9['amount'];
if ($amount == 0)
{
echo "<b>Balance: €00.00</b>";
}
else
{
echo "<b>Balance: $amount</b>";
}
}
You are adding extra $ to the $amount, try this:
if ($amount == 0) {
echo "<b>Balance: €00.00</b>";
} else {
echo "<b>Balance: $amount</b>";
}
In fact you can make your code a bit more readable/standard like this:
if ($amount == 0)
{
echo "<b>Balance: €00.00</b>";
}
else
{
echo "<b>Balance: $amount</b>";
}
I've moved the if statement inside the while loop, cleaned up the code and removed the extra $ sign that was on the last line.
while ($row9 = mysql_fetch_array($result9)) {
if ($row9['amount']) == 0 {
echo "<b>Balance: €00.00</b>";
} else {
echo "<b>Balance: $row9['amount']</b>";
}
}

Categories