So, Well I was wondering, Is it possible to do this;
if($pro == true)
echo "He's";
echo "a";
echo "pro.";
Or do I need to use { }
? Thanks.
It depends on your intentions. With your code
if($pro == true)
echo "He's";
echo "a";
echo "pro.";
The result (if $pro is true) will be:
He'sapro.
If $pro is false it will be
apro.
If you don't want that output on false, please add the braces.
if($pro == true) {
echo "He's ";
echo "a ";
echo "pro.";
}
questioner I think it's simple if else structure we all know from inception of coding.
After executing first line after IF block without bracket( '{' ), rest will be executed as it is.
if(1==1)
echo "Hi!";
echo "How are you?";
So the output of the above is: Hi! How are you?
Both lines are returned.
Now if you want to separate out output then enclose else block,
if(1==1) { echo "Hi!"; } else { echo "How are you?"; }
So the output of the above is: Hi!
#SpongePablo has truly explained well.
Related
I have a htm file that passes a variable called $team to a php file. The php file echo's 'Fred' just fine on line 3 if that is what the user inputs but an if statement which asks if $_POST["team"] == "Fred" always returns negative. Can anyone help?
<?php
echo $_POST["team"] , "<br />";
if ($_POST["team"] == "Fred"){
echo "You go for " , $_POST["team"];
}
else {
echo "You do NOT go for Fred";
}
?>
Output:
Fred
You do NOT go for Fred
I think $_POST["team"] has spaces. Try this:
<?php
echo $_POST["team"] , "<br />";
if (trim($_POST["team"]) == "Fred"){
echo "You go for " , $_POST["team"];
}
else {
echo "You do NOT go for Fred";
}
?>
Note: this code is not related to the (,) Because you using echo, not merge string
If your $_POST['team'] contains spaces, the comparison will always return false.
Try to use trim() (Documentation here). Trim remove all spaces in your string, so it will reduce chances to fall in errors like these.
<?php
$team = trim($_POST['team']);
echo $team , "<br />";
if ($team == "Fred"){
echo "You go for " , $_POST["team"];
}
else {
echo "You do NOT go for Fred";
}
?>
Let me know if it works =D
you can echo like this because merge string in php use .
if ($_POST["team"] == "Fred"){
echo "You go for " . $_POST["team"];
}
This is my code:
while($row = $result->fetch_array()){
$restext = $row['pagetext'];
echo "<br /><br />$restext <br /><br />";
$user = str_replace('[TD="align: left"]'.$uid.'[/TD]','<b>LOOLOLOOLOLOLO</b>', $restext);
if (strpos($user,'<b>LOOLOLOOLOLOLO</b>')===false) {
$test = '[TD="align: left"]'.$num.'[/TD] [TD="align: left"][/TD]';
if (strpos($test, $restext)!==false) {
echo "Base not reserved!";
}
else
echo "This base is already reserved!";
}
else
echo "You have already reserved a base!";
}
The problem is with the last part where I use this:
if (strpos($test, $restext)!==false) {
echo "Base not reserved!";
}
else
echo "This base is already reserved!";
It just doesn't work.
That code always outputs: This base is already reserved!
Even if I know it should be outputting Base not reserved!
Does anyone know why this is happening?
The debugging I have done:
I am able to echo $rextext and $test
If I change the if statement to: if (strpos($test, "blah blah the text goes here")!==false) {
It will work but I can't keep it like that because the text is dynamically pulled from the dB.
Thanks!
I am new to PHP and learning. I'm trying to pass a value through a url link but it doesn't seem to work.
The link value I am passing is http://www.mysite.com/index.php?id=f
I want to run a js script if ID not F seen below but right now when I run it. It doesn't do anything:
<?php
$ShowDeskTop = $_GET['id'];
if (isset($ShowDeskTop)){
echo $ShowDeskTop;
if ($ShowDeskTop != "f"){
echo "ShowDeskTop Value is not F";
echo "<script type=\"text/javascript\">";
echo "if (screen.width<800)";
echo "{";
echo "window.location=\"../mobile/index.php\"";
echo "}";
echo "</script>";
};
};
?>
I know this is easy PHP 101 but I can't figure it out. I have tried everything from w3schools to other sites on Google for the answer and having no luck. Could someone please tell me what I am doing wrong?
Thank you!
$ShowDeskTop is not the same as $ShowDesktop variables names are case sensitive!
This is never gonna work since you set the variable AFTER checking if it exist..
The most easy way:
<?php
if (isset($_GET['id'])) {
echo $_GET['id'];
if ($_GET['id'] != 'f') {
?>
<script type="text/javascript">
if (screen.width < 800) {
window.location = "../mobile/index.php";
}
</script>
<?php
}
}
?>
I don't think <> is valid in PHP (it is in VB.NET ..) the is not operator is != or !== (strict/loose comparison).
Also you don't have to close if statements with a ;
This:
if (expr) {
}
Is valid and not this:
if (expr) {
};
I thought about writing != instead of <>.
You have a number of problems including bad variable case (i.e. variables not matching), checking for variables before they exist, etc. You can simply do something like this:
if (!empty($_GET['id'])) { // note I check for $_GET['id'] value here not $ShowDeskTop
$ShowDeskTop = $_GET['id'];
echo $ShowDeskTop; // note I change case here
if ($ShowDeskTop !== "f"){ // note the use of strict comparison operator here
echo "YES, the id doesn't = f";
echo "<script type=\"text/javascript\">";
echo "if (screen.width<800)";
echo "{";
echo "window.location=\"../mobile/index.php\"";
echo "}";
echo "</script>";
} // note the removal of semicolon here it is not needed and is bad coding practice in PHP - this is basically just an empty line of code
} // removed semicolon here as well
Fist thing, you need ; at the end of echo $ShowDesktop
And, what does f mean in if ($ShowDeskTop <> "f"){
use strcmp() instead of <> operator.
Try
if(!strcmp($ShowDeskTop, "f")){
echo "YES, the id doesn't = f";
}
<?php
$ShowDeskTop = $_GET['id']; // assign before checking
if (isset($ShowDeskTop)){
//echo $ShowDeskTop;
if ($ShowDeskTop !== "f"){
echo "YES, the id doesn't = f";
echo "<script type='text/javascript'>";
echo "if (screen.width<800)";
echo "{";
echo "window.location.replace('../mobile/index.php');"; // assuming your path is correct
echo "}";
echo "</script>";
}
}
?>
I have a variable that is posted through a html form:
$_POST['ref']
And a variable that is pulled from a table in a database:
$row['ref']
i have a basic comparison script to check if they are both the same:
$ref = $_POST['ref'];
$result = mysql_query("SELECT * FROM logbook.job");
if (!$result) {
die("Query to show fields from table failed");
}
$row = mysql_fetch_array($result);
$refdb = $row['ref'];
$refform = $_POST['ref'];
echo $_POST['ref'] ."<br>". $row['ref'] . "<br><br>";
if ($refdb == $refform) {
echo "Yes they are<br><br>";
}
else {
echo "No they are not<br><br>";
}
if (is_string($_POST['ref']))
{
echo "Yes";
} else {
echo "No";
}
echo "<br>";
if (is_string($row['ref']))
{
echo "Yes";
} else {
echo "No";
}
Which outputs:
G2mtxW
G2mtxW
No they are not
Yes
Yes
I echo them both out. Than i ask if they are the same. Then i check whether each is a string.
How come they are not the same? How can i get them to match
Any help would be appreciated
Try using the binary-safe comparison for String:
result = strcmp($str1, $str2);
If the result is 0, then both are the same. Otherwise, they aren't.
One of your strings (probably the one from the DB) might be null-terminated. I've tested the following
$foo = "abc\0";
$bar = "abc";
echo "$foo\n$bar\n";
if($foo == $bar)
echo "Equal.";
else
echo "Not equal."
Output is
abc
abc
Not equal.
Try var_dump-ing both values, check their lengths and inspect them using view-source. They are different in someway.
echo $status_message;
echo "Accepted";
if(strcmp($status_message,"Accepted")==0)
{
echo "equal";
}
else
{
echo "not equal";
}
?>
$row['status'] is a field from table
for some r.eason I cant display a logged in users name when they are logged in? the code is below
<?php
if (isset($_SESSION['user_id'])) {
echo '<?php if (isset($_SESSION[\'first_name\'])) { echo ", {$_SESSION[\'first_name\']}!"; } ?>';
if ($_SESSION['user_level'] == 1) {
echo 'something else';
}
} else { echo 'something';
}
?>
Thanks every one but i solved it.
Ack! Just look at your code. Do you know what this line is doing?
echo '<?php if (isset($_SESSION[\'first_name\'])) { echo ", {$_SESSION[\'first_name\']}!"; } ?>';
That's so wrong I don't even know where to begin. Just try
echo $_SESSION['first_name'];
And see if that gets you closer to what you want ;)
Make sure you're also calling session_start() before trying to access the variables.
Change your code to:
<?php
session_start();
if (isset($_SESSION['user_id'])) {
if (isset($_SESSION['first_name'])) {
echo ", " . $_SESSION['first_name']} . '!';
if ($_SESSION['user_level'] == 1) {
echo 'something else';
}
} else {
echo 'something';
}
?>
This is not a valid PHP code. Single quote "'" are not pair up. The block ('{' and '}') are also not pairing up.
The most importantly, the code to show the first name is in a string so it will not be shown.
I think the code you are trying to write is:
<?php
if (isset($_SESSION['user_id'])) {
if (isset($_SESSION['first_name'])) {
echo ", {$_SESSION['first_name']}!";
}
if ($_SESSION['user_level'] == 1) {
echo 'something else';
}
} else {
echo 'something';
}
?>l
Is it?
Here are the list of possibilities of the mistakes and make sure that you have corrected them
1) have you set the cookie "first_name" using setcookie method...?
2) Then have u called the session_start() function so that the session variables can be called in that page??
3) Try echo $_SESSION['first_name']... i don understand why you have put the flower brackets coz i never have used them even once in my 15 php projects..