I have a question about an if statement.
if($_POST['billing_first_name'] == $tet['data']['0']['first_name']) {
}
else
{
run code
}
This code compares a string that it gets from a form with an already existing string.
It works. But when i dont add a capital letter, it runs the code after all.
So for example if i compare String with String it doesnt run the code (which is what i want) But when i compare string with String it runs the code (which i dont want) Only because theres no capital letter included at the beginning. Is there a way to fix this?
Based on the comment the code is running fine it is an understanding of which block your are in.
if(condition){//code} will only run code when the condition is true ie
if("String" == "String"){echo "foo";} // will echo foo
if("string" == "String"){echo "foo";} // will not echo foo
In order to run code when false you add an else
// Will echo foo
if("String" == "String"){
echo "foo";
} else {
echo "bar";
}
// Will echo bar
if("string" == "String"){
echo "foo";
} else {
echo "bar";
}
A trick to getting ifs to execute when something is false is to use the ! (meaning not)
// Will echo bar
if("String" != "String"){
echo "foo";
} else {
echo "bar";
}
// Will echo foo
if("string" != "String"){
echo "foo";
} else {
echo "bar";
}
I solved it thanks to cmorrissey.
using strlower on both variables allows me to compare 2 strings no matter if there are capital letters inside one of the strings.
if(strtolower($_POST['billing_first_name']) == strtolower($tet['data']['0']['first_name'])) {
//do nothing
}else {
//run code
}
Related
Firstly, I wrote this code, but it's too long...
if ($option1 == 'true')
{
echo "it works";
}
if ($option2 == 'true')
{
echo "it works";
}
if ($option3 == 'true')
{
echo "it works";
}
if ($option4 == 'true')
{
echo "it works";
}
if ($option5 == 'true')
{
echo "it works";
}
if ($option6 == 'true')
{
echo "it works";
}
if ($option7 == 'true')
{
echo "it works";
}
So, I want to improve code and have something like this, but it doesn't work. I can't understand how to change the name of a variable of if inside the for.
Thanks for help.
for ($i = 1; $i <= 7; $i++)
{
if ($option($i) == "true")
{
echo "it works";
}
}
}
PHP has a feature called variable variables that you can use to dynamically reference variables based on string names. I wouldn't recommend using them because it leads to difficult to understand code later on. https://www.php.net/manual/en/language.variables.variable.php
Use curly braces to name a variable after a constructed string:
if ( ${"option$i"} == true ) …
… but as stated in the comments, if what you want to do is INDEXING a collection of variables using a incremental number, you want to use regular arrays, especially if you plan to move to other languages (like C) that don't support this way of referencing.
PHP also supports associative arrays which enables you to use a string rather than a number as an index, which makes them key->value pair collections.
This is a repeat question of Check $_POST['id'] is numeric then do this if not do otherwise
But..
The answers given do not answer the question IMHO.
The question is testing a $_POST string to see if it is a number or contains non-numeric characters. Not change the code to use $_GET.
The original question is as such:
if (!empty($_POST['id'])) {
echo "empty";
} else {
if (is_numeric($_POST['id'])) {
echo "numeric!";
} else {
echo "not empty but not numeric how come?";
}
}
I have tried, is_numeric() , but since $_POST delivers the variable as a string, even if it is a number this is useless.
I have tried, if(!preg_match('#[^0-9]#',$id)), but I have no idea why this doesn't work.
ctype_digit() I believe still has a problem with seeing all things from $_POST as strings, so no good.
There has to be a way, but I'm lost on how to do this. And no I can't use $_GET.
UPDATE ANSWER!!! (TYPO!!!!! god dammit!!!!):
// test.php
// testing with $_POST['id'] from forum with id = 5 and another test where id = new
$id = $_POST['editid'] ;
echo "<br>---".$id."---<br>";
if (empty($id)) {
echo "<br>1: empty";
} else {
if (!is_numeric($id)) {
echo "<br>2: This is the number 5";
} else {
echo "<br>3: the must be the word new";
}
}
// test 2 ... ctype_digit
if (empty($id)) {
echo "<br>4: empty";
} else {
if (!ctype_digit($id)) {
echo "<br>5: This is the number 5";
} else {
echo "<br>6: the must be the word new";
}
}
// test 3 ...
if (empty($id)) {
echo "<br>7: empty";
} else {
if (!preg_match('#[^0-9]#',$id)) {
echo "<br>8: This is the number 5";
} else {
echo "<br>9: the must be the word new";
}
}
/**
result from 5
---5---
3: the must be the word new
6: the must be the word new
8: This is the number 5
results from "new"
**/
When the user introduces a letter and clicks on submit the script shall return a name that starts with that letter, I wrote it using a Switch/Case structure, now I need to write it using if/elseif/else.
The problem is that no matter what letter I introduce on the Textbox I'll always get the return for A (Aberdefia - Anacleto)
$nombrador=$_POST['nombrador'];
$nombrador=strtoupper($nombrador);
if ($nombradorIf = 'A') {
echo "Aberdefia - Anacleto";
} elseif ($nombradorIf = 'B') {
echo "Brígida - Brígido";
} else {
echo "Is that even a letter?";
}
The code for the letters C to Z is just like the one for A and B.
You need == for comparison, = is for assignment, and === is for identical or same type.
Also, you used $nombrador when assigning and $nombradorIf when comparing, resulting in an undefined variable.
if ($nombrador == 'A') {
echo "Aberdefia - Anacleto";
} elseif ($nombrador == 'B') {
echo "Brígida - Brígido";
} else {
echo "Is that even a letter?";
}
Thus, $nombrador == 'A' means $nombrador is equal to A.
And, $nombrador = 'A' means to assign A to $nombrador.
More information at http://php.net/manual/en/language.operators.comparison.php.
Hope this helps, thanks!
== is the conditional operator = is assigning operator
if ($nombrador == 'A') {
echo "Aberdefia - Anacleto";
} elseif ($nombrador == 'B') {
echo "Brígida - Brígido";
} else {
echo "Is that even a letter?";
}
Try this code
$nombrador=$_POST['nombrador'];
$nombrador=strtoupper($nombrador);
if ($nombrador == 'A') {
echo "Aberdefia - Anacleto";
} else if($nombrador == 'B') {
echo "Brígida - Brígido";
} else {
echo "Is that even a letter?";
}
Your code error near . you condition check variable name is $nombradorIf and also not declare variable but You assign the value variable name is $nombrador and you not use conditional operator == . so result not proper . You use above the code working fine
For checking conditional statement you have to use == (double equal sign) like below:
$nombrador=$_POST['nombrador'];
$nombrador=strtoupper($nombrador);
if ($nombrador == 'A') {
// ^^ Use like this
echo "Aberdefia - Anacleto";
} elseif ($nombrador == 'B') {
// ^^ Use like this
echo "Brígida - Brígido";
} else {
echo "Is that even a letter?";
}
I did a if thing to check if the variable only had aplha caracter but I also want to check if the variable strated with a vowel.
if (ctype_alpha($original)) {
print "oké";
}
else {
print "pas oké";
}
If there isn't any specific function, how could I know ?
Thank you
$vocals = array('a','e','i','o','u');
if (ctype_alpha($original) && in_array($original{0}, $vocals)) {
print "oké";
} else {
print "pas oké";
}
With $string{index} you could access to a char into a certain position into $string.
With in_array you could check if that letter is contained into an array (in our example, vocals array)
Alternative solution
If you wish, you could even use preg_match that will perform test with regular expressions
if (ctype_alpha($original) && preg_match('/^[aeiou]/i', $original))
Working as of PHP 7.4:
$vowels = array('a','e','i','o','u');
if (ctype_alpha($original) && in_array($original[0], $vowels)) {
print "oké";
} else {
print "pas oké";
}
I've some problems with handling Boolean values in PHP. It is a validation script before storing data into database. I wrote a global validator that will validate and return a Boolean value whether the validation was successful .
Here is my code.
//VALIDATE
$isValid = true;
foreach($team as $key=>$val) {
if(!is_array($val)){
$isValid = $isValid && validate($val, $key);
}
}
for($it=0;$it<count($team['members']);$it++){
foreach($team['members'][$it] as $key=>$val) {
$isValid = $isValid && validate($val, $key);
}
}
if(!$isValid) { // EDITED: if(!isValid)
echo "validation error";
exit(1);
}
//END OF VALIDATE
The validate function is working properly but sometimes I end up getting $isValid = true or the other way, when I try with some test cases.
Hmm.. What am I doing wrong here ?
Please check, if this form does the trick:
if( false === $isValid) {
echo "validation error";
exit(1);
}
Note, that ( ! $isValid ) or (false == $isValid ) in some cases return results, which are at first look wrong. See for example the hint in the strpos() documentation.
In fact, the results are fine, since operations line ! or == try to cast operands in a 'useful' way.
That said, it's always better to user the === operator, since it checks values and types of operands. Please see operator overview.
if(!isValid) { falls back to if (!"isValid"), if there is no constant isValid. You probably meant if (!$isValid) {.
if(!isValid) {
isValid has no dolar, (you need to give variables in PHP some cash) so:
if(!$isValid) {
Source : http://bit.ly/1hxDmVR
Here is sample code for working with logical operators in PHP. Hope it will helpful:
<html>
<head>
<title>Logical</title>
</head>
<body>
<?php
$a=10;
$b=20;
if($a>$b)
{
echo " A is Greater";
}
elseif($a<$b)
{
echo " A is lesser";
}
else
{
echo "A and B are equal";
}
?>
<?php
$c=30;
$d=40;
//if(($a<$c)AND($b<$d))
if(($a<$c)&&($b<$d))
{
echo "A and B are larger";
}
if(isset($d))
$d=100;
echo $d;
unset($d);
?>
<?php
$var1=2;
switch($var1)
{
case 1:echo "var1 is 1";
break;
case 2:echo "var1 is 2";
break;
case 3:echo "var1 is 3";
break;
default:echo "var1 is unknown";
}
?>
</body>
</html>
I think the problem is that your $isValid variable can be changed many times in the loops and by the end of your code simply applies to the last value in your final loop.
You should set it to true initially and then only set it to false IF your validity check fails - not simply assign its value based on every single validity check.