IF statement newbie [duplicate] - php

This question already has answers here:
The 3 different equals
(5 answers)
Closed 8 years ago.
<?php
if ($user->getProfile()->get('title')="Canon"); {
echo "Test1"; }
else {
echo "Test2"; }
?>
This is causing my site to break, is there an obvious mistake?
Thank you.

= is an assignment, you want == for a comparison.
You also shouldn't have a ; between ) and {.

This will work:
<?php
if ( ($user->getProfile()->get('title')) == "Canon" ){
echo "Test1";
}
else {
echo "Test2";
}
?>
First of all You need a comparison '==' and not an assigning '='
And then You have a syntax error with a ';' after the condition

You need to change the = sign to == as the first one assigns a values while the latter one compares it.
And also, you don't need to terminate the if statement with a semicolon
if ($user->getProfile()->get('title') == "Canon") /* note that there is no semicolon here */
{ echo "Test1"; }
else
{ echo "Test2"; }

<?php
if ($user->getProfile()->get('title')=="Canon") {
echo "Test1";
}
else {
echo "Test2";
}
?>
Try this.

Related

Problem during creating Pyramid Star Patterns in PHP [duplicate]

This question already has answers here:
PHP Displaying a number of asterisks depending on the number given in the input field
(4 answers)
Closed 11 months ago.
Question :
My Code :
<html\>
Enter the number of rows:
<?php
if($_POST)
{
$row = $_POST['row'];
if(!is_numeric($row))
{
echo "Make sure you enter a NUMBER";
return;
}
else
{
for($row=0;$row<=1;$row++){
for($j=0;$j<=$row;$j++){ echo "#"; } echo ""; }}}?>
The problem is it's showing only two rows
I expected as shown in the photo
$row = 10;
for($i=0;$i<=$row;$i++){
for($j=0;$j<=$i;$j++){ echo "#"; } echo "<br>"; }
You are overriding the $row variable.
Also, you don't need the else since you are returning in case the if(!is_numeric) is true;
This should do it.
if(isset($_POST['row'])) {
$rows = $_POST['row'];
if(!is_numeric($rows))
{
echo "Make sure you enter a NUMBER";
return;
}
for($row=0;$row<=$rows;$row++){
for($j=0;$j<=$row;$j++){
echo "#";
}
echo "\n";
}
}
You can make use of the PHP function str_repeat to simplify the script.
This would only take 1 loop, so you don't get confused with the variable names.
$row = 10;
$i = 1;
while ($i <= $row)
{
echo str_repeat("#", $i); echo "\n";
$i ++;
}
Working Example here.

PHP giving same output for different inputs [duplicate]

This question already has answers here:
The 3 different equals
(5 answers)
PHP if single or double equals
(3 answers)
Closed 2 years ago.
I am trying to create a simple demonstration of web services where I retrieve price of book. But here when I try different book names it gives price of 1st book entered in array.
<?php
function get_price($find){
$books=array(
"c"=>20,
"java"=>30,
"php"=>40
);
foreach ($books as $book=>$price) {
if ($book=$find) {
echo $book;
echo $price;
return $price;
break;
}
}
}
?>
Here, if I enter 'java' it gives price of 'c'.
<?php
//process client req
header("Content-Type:application/json");
include("functions.php");
if (!empty($_GET['name'])) {
# code...
$name=$_GET['name'];
$price=get_price($name);
if(empty($price))
deliver_response(200,"Book not Found",NULL);
else
deliver_response(200,"Book Found",$price);
}
else
{
//throw error
deliver_response(400,"Invalid",NULL);
}
function deliver_response($status,$status_message,$data){
header("HTTP/1.1 $status $status_message");
$response['status']=$status;
$response['status_message']=$status_message;
$response['data']=$data;
$json_response=json_encode($response);
echo $json_response;
}
?>
Can anyone help??
= is the assignment operator. If you want to check for equality, you should use the == operator (or better yet, === to avoid type juggling shenanigans):
foreach ($books as $book=>$price) {
if ($book == $find) {
// Here^
echo $book;
echo $price;
return $price;
break;
}
}
please change the line
if ($book=$find) {
to
if ($book==$find) {
Change if ($book=$find) to if ($book == $find). You're using the assignment operator (=) instead of a comparison operator (== or ===).

PHP syntax error, unexpected 'elseif' [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I get this error:
Parse error: syntax error, unexpected 'elseif' (T_ELSEIF) in C:\...\functions.php on line 6
This is the whole content of functions.php:
<?php
// prints the suitable string based on the chosen language
function lecho ($cs,$sk,$en) {
global $lang;
if ($lang=="cs") echo $cs;​
elseif ($lang=="​sk") echo $​sk;​
elseif ($lang=="​en") echo $​en;​​ // line 7
}
?>
What's wrong? This seams like such a basic thing!
You're just missing a space: it's else if, not elseif.
You need to use {'code here'} after an if/elseif/else statement. The correct way to go about this would be:
<?php
// prints the suitable string based on the chosen language
function lecho ($cs,$sk,$en) {
global $lang;
if ($lang=="cs") {
echo $cs;​
} elseif ($lang=="​sk") {
echo $​sk;​
} elseif ($lang=="​en") {
echo $​en;​​
}
}
?>
And it is simple. You just need to correct syntax for if/elseif
<?php
// prints the suitable string based on the chosen language
function lecho ($cs,$sk,$en) {
global $lang;
if ($lang=="cs")
{
echo $cs;​
}
elseif ($lang=="​sk")
{
echo $​sk;​
}
elseif ($lang=="​en")
{
echo $​en;​​ // line 7
}
}
?>

Correct format of PHP if block [duplicate]

This question already has answers here:
PHP logical AND operator error [closed]
(5 answers)
Closed 9 years ago.
Complete PHP noob here with an error returning because of a big nested if block. The error varies as I change the code, but it is always to do with the if statement. What is the correct way to do this statement?
Here is the incorrect if block:
if(!empty($_GET['x2'])) and (!empty($_GET['x'])) and (!empty($_GET['num']))
{
$xsqrd = $_GET['x2'];
$x = $_GET['x'];
$num = $_GET['num'];
if(($xsqrd*2)==0.0)
{
print $errdivzero;
}
else if(sqrt(pow($x,2)-(4*$xsqrd*$num)))
{
print $errsqrtmin1;
}
else
{
$ans1 = (-$x)+(sqrt(pow($x,2)-(4*$xsqrd*$num)))/(2*$xsqrd);
$ans2 = (-$x)-(sqrt(pow($x,2)-(4*$xsqrd*$num)))/(2*$xsqrd);
}
}
if(!empty($_GET['x2']) && !empty($_GET['x']) && !empty($_GET['num']))
{
$xsqrd = $_GET['x2'];
$x = $_GET['x'];
$num = $_GET['num'];
if(($xsqrd*2)==0.0)
{
print $errdivzero;
}
else if(sqrt(pow($x,2)-(4*$xsqrd*$num)))
{
print $errsqrtmin1;
}
else
{
$ans1 = (-$x)+(sqrt(pow($x,2)-(4*$xsqrd*$num)))/(2*$xsqrd);
$ans2 = (-$x)-(sqrt(pow($x,2)-(4*$xsqrd*$num)))/(2*$xsqrd);
}
}
You have extra ( in if condition and also you declared and which is not php function. you need to replace and with &&.
Now it's working fine without any error message.
if(!empty($_GET['x2']) && (!empty($_GET['x']) && (!empty($_GET['num']))
// ^ extra bracket ^ extra bracket
{
$xsqrd = $_GET['x2'];
$x = $_GET['x'];
$num = $_GET['num'];
if(($xsqrd*2)==0.0)
{
print $errdivzero;
}
else if(sqrt(pow($x,2)-(4*$xsqrd*$num)))
{
print $errsqrtmin1;
}
else
{
$ans1 = (-$x)+(sqrt(pow($x,2)-(4*$xsqrd*$num)))/(2*$xsqrd);
$ans2 = (-$x)-(sqrt(pow($x,2)-(4*$xsqrd*$num)))/(2*$xsqrd);
}
}
You had extra closing brackets at the position highlighted above. Also replace and with &&
Well let's check your brackets:
if(!empty($_GET['x2'])) and (!empty($_GET['x'])) and (!empty($_GET['num']))
^ ^
The second highlighted bracket closes the if statement - this will cause an error, you should remove unnecessary brackets like so::
if(!empty($_GET['x2']) and !empty($_GET['x']) and !empty($_GET['num']))
In future you should carefully check that for each opening bracket the closing bracket is where you expect it to be, and don't go overboard with the number of brackets.

Simple syntax help for an IF statement

Hello I have a foreach loop that assigns values to the following code
${'varimages' . $image_count} = $images;
// outputs for image_count = 1, $varimages1 = a string
However, I want to make an IF statement to check something. The "correct" form should be below but I have errors for the presence of "{" and "}"
if (${'varimages' . $image_count} == ${'vartitle' . $image_count})
{ echo "ok"; } else { echo "not ok"; }
How can I rewrite that statement correctly? Thank u!
Using arrays, it would be:
if($varimages[$image_count] == $vartitle[$image_count])

Categories