PHP | parse error T_DOUBLE_ARROW [duplicate] - php

This question already has answers here:
unexpected T_DOUBLE_ARROW [closed]
(2 answers)
Closed 6 years ago.
Hi i'm learning some basic php, and i'm having some a parse error. It says that the error is located on line 8(if($book=>$find)). What is wrong with line 8?
function getPrice($find)
{
$books = array ("java"=>299,"c"=>348,"php"=>267);
foreach ($books as $book=>$price)
{
if($book=>$find)
{
return $price;
break;
}
}
}
thank you in advance :D

Use == or === to compare change here
if($book == $find)
Also no need to write break after return here
return $price;
break;
By the way you can also write your code like this
<?php
function getPrice($find)
{
$books = array ("java"=>299,"c"=>348,"php"=>267);
if(isset($books[$find])){
return $books[$find];
}
return false;
}
echo getPrice("java");
?>
Check here : https://eval.in/592069

Your syntax:
if($book=>$find)
is incorrect.
From what I see, it seems like you want to find a book which matches $find.
Try this instead:
if ($book==$find)

Related

PHP in_array() returning false always [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
How can I get useful error messages in PHP?
(41 answers)
Closed 3 years ago.
I'm trying to check if a value is in a array but it's always returning false. I've tried to fix this in quite a few different ways but none worked.
I have this file which I used require_once '../path/'; to add it to my current script. It is a JSON that was converted to PHP nested arrays.
This is the function that is always returning false. I did a lot of testing using echo and everything looks fine with $states_json and the array $cities.
If anyone could help me with this situation I would be apprciated.
EDIT: I'm calling this function with validateInstCity("RS", "Porto Alegre") so it was supposed to return true. After some more testing, I found out that the problem is that $states_json is NULL within the function. The strange part is that I used it inside others functions before without any problems. As you may see on the file, when using validateInstCity("RS", "Porto Alegre") $idx should be 22 and the function should return true.
function validateInstCity($inst_province = null, $inst_city = null) {
if (empty($inst_province) ||
empty($inst_city)) {
}
$idx;
for ($i=0; $i < count($states_json); $i++) {
if ($states_json[$i]['sigla'] == $inst_province) {
$idx = $i;
break;
}
}
$cities= array();
for ($i=0; $i < count($states_json[$idx]['cidades']); $i++) {
array_push($cities, $states_json[$idx]['cidades'][$i]);
}
if (in_array($inst_city, $cities, false)) {
return true;
} else {
return false;
}
}

API Call not returning error when parameter data is invalid [duplicate]

This question already has answers here:
The 3 different equals
(5 answers)
Closed 5 years ago.
I'm currently experimenting with PHP GET Requests, But stuck with this.
Please remember i haven't really experimented with this.
So it's rather now to me,
To the point..
I'm trying to return an invalid argument message if the user does not specify
one of the following arguments
Been tinkering with this for quite a bit,
But unable to really figure it out.
?param=first or ?param=second
But it returns it returns the valid parameter message anyway.
Any suggestions to this?
I'll drop my code below!
<?php
$param = $_GET['param'];
if (empty($_GET['param']))
{
print('<h2>Parameter param is not set!<h2>');
print '<br>';
print('<h5><font color="red">Proper usage: http://localhost/zoek.php/?param=first</font></h5>'); //first
print('<h5><font color="red">Or second http://localhost/zoek.php/?param=second</font></h5>'); //second
die();
}
?>
<?php
if ($param == 'first')
{
print('Do something when url = http://localhost/zoek.php/?focus=first');
//do more stuff
}
else if ($param = 'second')
{
print('Do something when url = http://localhost/zoek.php/?focus=second');
//do more stuff.
}
//attempt to return error message when parameter $param is not first or second
else
{
print('The by you specified argument '. $_GET['param'] .' is invalid.');
die();
}
?>
The problem is with this line:
else if ($param = 'second')
It should be == instead of =.

Why can't you use an inline if statement to return? [duplicate]

This question already has answers here:
return statement in ternary operator c++
(5 answers)
Closed 6 years ago.
I seems that there is a limitation to using an inline if statement in PHP when you want to return.
function example($Variable)
{
( (int) (++$Variable) == 1 ) ? return true : return false;
}
$test = example(1);
Expected: false
This gives an error which is strange because I have never seen this before.
Error type : 4
Message : syntax error, unexpected 'return' (T_RETURN)
Can anyone explain why you cannot use the return inside an if statement like this?
The ternary already returns the results. You can't return a return function.
You can make return before variable result like this:
function example($Variable)
{
return ( (int) (++$Variable) == 1 ) ? true : false;
}
$test = example(1);

Error in syntax for if statement in codeigniter? [duplicate]

This question already has answers here:
Weird PHP error: 'Can't use function return value in write context'
(12 answers)
Closed 9 years ago.
EDIT FOR THE ADMINS: IT IS NOT THE SAME QUESTION AS THE ONE ALREADY ASKED, SINCE THE ORIGIN OF THE ISSUE IS DIFFERENT!!!
I am trying to display the current language selected, which is saved in the sessions table.
What I did first was the simple statement:
<?php echo $this->session->userdata("language"); ?>
which works quite well. The problem here is, that the language is saved into the session table in English and lower case, means: "english", "german", "spanish", etc
I then tried to resolve this using an if statement as follows:
<?php if ($this->session->userdata("language") = spanish) { echo 'Español'; } else if ($this->session->userdata("language") = english) { echo 'English'; } else echo 'Deutsch'; ?>
unfortunately, this returns:
Fatal error: Can't use method return value in write context in
/home/.../.../.../app/views/header.php on line 270
Any hint on what I am doing wrong? Thanks for your quick help ;)
You need to use a comparison operator == (I'm sure your = is just the usual common typo), since you can't assign a value (write) to the $this->session->userdata('anything') (method return), i.e.
Can't use method return value in write context
<?php if ($this->session->userdata("language") == 'spanish') {
echo 'Español';
}
elseif($this->session->userdata("language") == 'english') {
echo 'English';
}
else echo 'Deutsch';
?>
== operator and quoted strings should solve it:
<?php
if ($this->session->userdata("language") == 'spanish') { echo 'Español'; }
else if ($this->session->userdata("language") == 'english') { echo 'English'; }
else echo 'Deutsch';
?>
Make use of a SWITCH Statement to achieve this [Code will really look readable]
<?php
$language=$this->session->userdata("language");
switch($language)
{
case "spanish":
echo 'Español';
break;
case "english":
echo 'English';
break;
//.... goes on
}
To compare two things you should be using the == Operator. Also string values should be wrapped into quotes "myStringValue".

Creating default object from empty value, how to define a value [duplicate]

This question already has answers here:
Creating default object from empty value in PHP?
(18 answers)
Closed 8 years ago.
I am getting an warning message in my Wordpress :
"Creating default object from empty value in
/home/forthemde/public_html/wp-content/themes/mayadeep/admin/admin.php
on line 225"
Here the code on admin.php and:
function upfw_setup_theme_options(){
$up_options_db = get_option('up_themes_'.UPTHEMES_SHORT_NAME);
global $up_options;
//Check if options are stored properly
if( isset($up_options_db) && is_array($up_options_db) ):
//Check array to an object
foreach ($up_options_db as $k => $v) {
$up_options -> {$k} = $v;
}
else:
do_action('upfw_theme_activation');
endif;
}
add_action('upfw_theme_init','upfw_setup_theme_options',10);
add_action('upfw_admin_init','upfw_setup_theme_options',100);
Line 225 is here:
$up_options -> {$k} = $v;
I try to fix by myself, but no result. Please help me, really appreciate for any help.
Regards.
I had this problem too, recently, but this worked for me:
Just added $up_options = new stdClass(); after global $up_options;
Best regards,
http://shubhinfotech.com http://c-worx.com

Categories