Here's what I came up with and wondering if there is a more eloquent way to accomplish the below or if this is acceptable?
Update using Ternary Operators:
$fbpid = ($_GET['fbt'] == 1) ? ("123456") : (NULL);
echo $fbpid;
What I originally had:
$fbt = $_GET['fbt'];
if ($fbt == 1) {
$fbpid = "123456";
}
else {$fbpid = NULL;}
echo $fbpid;
$fbt = $_GET['fbt'];
$fbpid = null;
if ($fbt == 1)
{
$fbpid = "123456";
}
echo $fpbid;
// output: 123456
Related
first of all, apologies for my bad english.
I try to set two vars as booleans, like this:
if ((strlen($imgCropperIPS) > 100) || (file_exists('files/'.$idIPS .'_ips.jpg'))) {
$logoIPS_temp = true;
} else {
$logoIPS_temp = false;
}
if ((strlen($imgCropperEmp) > 100) || (file_exists('files/'.$idIPS .'_emp.jpg'))) {
$logoEmp_temp = true;
} else {
$logoEmp_temp = false;
}
$data['logoBool'] = $logoEmp_temp . "-" . $logoIPS_temp;
$query = "UPDATE ips SET ipsName='$nameIPS', ipsNIT='$nitIPS', externalMant=$externalMant, empName='$nameEmp', empNIT='$nitEmp', ipsLogo= $logoIPS_temp, empLogo = $logoEmp_temp WHERE idIPS = '$idIPS' AND userAdmin='$userAdmin'";
$data['query'] = $query;
The server response this:
data['logoBool'] response: "-1"
data['query'] response:
"UPDATE ips SET ipsName='hospital', ipsNIT='848484', externalMant=true, empName='buss', empNIT='12312312', ipsLogo= 1, empLogo = WHERE idIPS = '1' AND userAdmin='1'"
The problem is with two vars "logoEmp_temp and logoIPS_temp"
Help.
Define vars in the start of document...
I solved it like this:
$temp_ips = "false";
$temp_emp = "false";
if ((strlen($imgCropperIPS) > 100) || (file_exists('files/'.$idIPS .'_ips.jpg'))) {
$temp_ips = "true";
} else {
$temp_ips= "false";
}
if ((strlen($imgCropperEmp) > 100) || (file_exists('files/'.$idIPS .'_emp.jpg'))) {
$temp_emp = "true";
} else {
$temp_emp = "false";
}
I'm fairly new to PHP so forgive me if this function is badly done.
I have a function:
function socialLink($sm_type = NULL) {
if ($sm_type = 'twitter') {
echo 'https://www.twitter.com';
} else {
echo 'https://www.facebook.com';
}
}
In my code when I call the function socialLink('facebook'); it echo's the Twitter URL.
Surely it should echo the Facebook URL since $sm_type would be equal to 'facebook' not twitter ?
Any help would be appreciated.
Set your if condition with this,
function socialLink($sm_type = NULL) {
if ($sm_type == 'twitter') {
echo 'https://www.twitter.com';
} else {
echo 'https://www.facebook.com';
}
}
See this.
function socialLink($sm_type = NULL) {
if ($sm_type == 'twitter') {
echo 'https://www.twitter.com';
} else {
echo 'https://www.facebook.com';
}
}
NOTE: Single = use to assign the value and = = use to compare values
Different's Between = , = = , = = =
= operator Used to just assign the value.
= = operator Used to just compares the values not datatype
= = = operator Used to Compare the values as well as datatype.
Your if statement does not use a comparison operator, it is an assignment (=). For a comparison, please use "==".
if ($sm_type == 'twitter') {
echo 'https://www.twitter.com';
} else {
echo 'https://www.facebook.com';
}
if ($sm_type == 'twitter') {
echo 'https://www.twitter.com';
} else {
echo 'https://www.facebook.com';
}
In php == is use for string comparison so, In this case you can't used = for that, simple :)
I have a form, and I want to validate it with PHP, for that I going to make just one if the problem is I want to detect where is the variable that equal to 0
My code
if(isset($_POST["submit"])){
$name = $_POST["name"];
$lname = $_POST["lname"];
if($name == "" || $lname == ""){
echo "Please, Enter All informations";
}
else {
$namepr = preg_match("/^[A-Za-z]{3,}$/",$name);
$lnamepr = preg_match("/^[A-Za-z]{3,}$/",$lname);
if($namepr == 0 || $lnamepr == 0){
<!-- here I want to select the variable that equal to 0 -->
}
else {
echo "Your name is : ".$name."<br>Your Last name is : ".$lname
}
}
}
my question is in the comment part, I want to show the variable equal to 0, I have two variable, I want to select the variable equal to 0
For your issue, you can use a ternary operator:
if($namepr == 0 || $lnamepr == 0){
$myVar = ($namepr == 0 ? $namepr : $lnamepr);
// do something with $myVar
}
or of course, a standard if block:
if($namepr == 0 || $lnamepr == 0){
if($namepr == 0){
$myVar = $namepr;
} else {
$myVar = $lnamepr;
}
// do something with $myVar
}
Good day guys,
I've made a sweet favorites function with php mysql and ajax, and its working great. Now I want to show 'favorite' when favorite = 0 and show 'unfavorite' when favorite = 1
if ($favorites == 0) {
$favorite = 'Favorite';
}
if ($favorites == 1) {
$unfavorite = 'unFavorite';
}
and echo it in the row as :
<div id="favorites">' .($favorite). ' ' .($unfavorite). '</div>
The problem is: when favorite = 0, both $favorite and $unfavorite are being shown. When favorite = 1 only $unfavorite is being shown correctly. Of course it should be $favorite OR $unfavorite. I assume the problem is clear and simple to you, please assist :)
Thanks in advance
It's easier to use just one variable:
$text = ''
if ($favorites == 0) {
$text = 'Favorite';
} else {
$text = 'unFavorite';
}
...
echo $text;
If you want to check $favorite, you are using the wrong variable in your control statement. Also, it is better coding practice to use elseif rather than if for that second if. One more thing: it's easier to manage one resulting variable.
$output = "";
if ($favorite == 0) {
$output = 'Favorite';
}
elseif ($favorite == 1) {
$output = 'unFavorite';
}
...
echo $output; // Or whatever you want to do with your output
Is $favorites an integer?
Anyway try using three equal signs (===) or else instead of the second if:
if ( $favorites === 0 )
{
// ...
}
else // or if ($favorites === 1)
{
// ...
}
You're making a toggle, so you only need one variable:
if(empty($favourites)){
$fav_toggle = 'Favorite';
} else {
$fav_toggle = 'unFavorite';
}
echo $fav_toggle;
Same code is working on me if I assigned $favorites = 0; or $favorites = 1;
You can also use if else
$favorites = 1;
if ($favorites == 0) {
$favorite = 'Favorite';
}
else if ($favorites == 1) {
$unfavorite = 'unFavorite';
}
function add_new($father,$chName) // add new category
{
if($father = "1" ) {
$result = mysql_query("INSERT into stinky_menu (title,nest_under)
VALUES('".$chName."','1')");
}
else {
$result = mysql_query("UPDATE stinky_menu SET title = '$chName' nest_under = '$father'");
}
}
I am getting the value of father from parent page, but its not going to else condition if its not equal to one.
You’re using the assignment operator = rather than the comparison operator ==. So try this:
if ($father == "1") {
// …
} else {
// …
}
That's because you have
if($father = "1")
You need to use "==". "=" is the assignment operator. You are setting $father equal to "1" even when it isn't.
Try:
if ($father == 1){}
Read here about comparison operators. "=" is the assignment operator.
Look at this to see what your code does:
<?php
$father = 55;
if ($father = 1){}
else{}
echo $father;
?>
This prints "1".
Also, should not that last query be:
"UPDATE stinky_menu SET title = '$chName', nest_under = '$father'"