Error when accessing array in PHP [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm trying to access the $_GET array in PHP like so:
<?php
$incl = $_GET("incl");
if ( $incl == "" ) { $incl = "home"; }
Whenever I access the script, I get a 500 error. Any idea why?

If you get an unexpected HTTP 500 error that you haven't sent yourself from PHP, this means there's an actual error in your script.
In this case, your problem is this line:
$incl = $_GET("incl");
$_GET is an array, so when you want to access values within it by key, you need to do so with [], not ():
$incl = $_GET['incl'];
Further, your check for $incl being empty should look like this:
if(empty($_GET['incl']){
$incl = 'home';
} else {
$incl = $_GET['incl'];
}

Related

PHP global array gives null inside function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I receive the following error message when I attempt to call a function which I need to push an object into an array:
array_push() expects parameter 1 to be array, null given
Any clues why this is happening? Thank you in advance :)
<?php
$programming = array();
//some unrelated lines of code here inbetween
function createProgramming($data){
global $programming;
$prog = new Programming($data);
array_push($programming, $prog);
}
?>
//random HTML here
<php?
createProgramming("str");
?>
//more html
$programming is only referenced in the code at those three locations present in my extract above.
That code works fine. There are few things that could make it break:
$programming is redefined / unset before createProgramming() is called
$programming is not defined in the global scope

Fatal error: Can't use function return value in write context in [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm getting this error and I can't make head or tail of it.
The exact error message is:
function kdrusha_theme_create_page() {
require_once(get_template_directory().= '/inc/pages/kdrusha-settings.php');
}
add_menu_page("KD Rusha Options", 'KD Rusha', 'manage_options', 'kdrusha-options', 'kdrusha_theme_create_page','',99);
The problem is that you're using .=.
something .= something_else
is shorthand for
something = something . something_else
But your something is a function call, and it generally doesn't make sense to assign to a function call (the exception is when it returns a reference).
You should just use ., which concatenates its parameters and returns the result without assigning it anywhere.
require_once(get_template_directory() . '/inc/pages/kdrusha-settings.php');
You need to put your function return in some variable:
function kdrusha_theme_create_page() {
$template = get_template_directory();
require_once($template.'/inc/pages/kdrusha-settings.php');
}

Pass variable into url parameter php [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying to pass a variable from a form to another php file to create an xml file.
<?php
$sentxt = $_POST['text']; //this comes from the form php
$params = array(
'answer_url' => 'to_xml.php?msg="$sentxt"',
);
$response = $p->make_xml($params);
echo "$sentxt";
?>
Whenever I try to run this I run into a problem
The xml file keeps outputting "$sentxt" instead of the string passed to the $sentxt via the php form post.
The echo "$sentxt"; displays the right string has been passed through properly, but the string is never passed into the array.
You should replace ' with " for string interpolation.
<?php
$sentxt = $_POST['text']; //this comes from the form php
$params = array(
'answer_url' => "to_xml.php?msg=$sentxt",
);
$response = $p->make_xml($params);
echo $sentxt;
?>

How to get a value from nested array? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Trying to fetch the first URL field from an array of them that comes from a JSON I have decoded but I get this error:
Parse error: syntax error, unexpected '[' in C:\blabla
foreach($data-> images as $data2) {
print_r(images[0]['url']);
}
I hope its enough of my code to work out what I am doing wrong?
Added: I would like the first "url" and it was getting the last one hence why I am changing the code and trying to debug it here.
Within your foreach you use the variable name you specified in the definition:
So something like...
foreach($data->images as $data2) {
print_r($data2[0]['url']);
}
Although, depending on the structure of the array, I'd imagine that you don't need the number, so it might be:
foreach($data->images as $data2) {
print_r($data2['url']);
}
If you wanted to loop through the values by a number, you'd use a for loop
for ($i = 0; $i <= count($data->images); $i++)
{
print_r($data->images[$i]);
}

php &variables in the URL aren't being defined in the page [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a messages page that loads communications between two users. The URL is message.php?u=[me]&p=[message parent id]&op=[other person], but $p and $op are not being defined in the page. When I echo each variable separately, $u appears everywhere from pre tag to the bottom of the document, but $p and $op do not echo anywhere. I tried deleting everything in .htaccess to see if that was causing a bug, but it wasn't. I can't think of what would cause this.
yourscript.php?var1=value1&var2=value2
You can use this by:
$var1 = $_GET['var1']; // $var1 = 'value1'
$var2 = $_GET['var2']; // $var2 = 'value2'
You can check whether or not these URL-parameters are set with isset()
(otherwise you create errors when you do not set them):
if (isset($_GET['var1'])) {
$var1 = $_GET['var1'];
} else {
die('usage: yourscript.php?var1=value1 !');
}

Categories