I had some code added that deals with canonical links but it doesn't seem to be working. One line of code that is repeated in several files shows a red mark next to it in Dreamweaver so i suspect this is wrong.
$pid = explode('=',explode('&',$_SERVER['QUERY_STRING'])[0]);
Can anyone see an obvious reason this would be flagged up by DW. I don't see any unclosed brackets or ' in it so i'm a bit lost.
Update:
It appears there is a fundamental error in the original code as the var $pid[1] is never given the correct data.
I tried using the split code answer below and if i print_r this
$qs = explode('&',$_SERVER['QUERY_STRING']);
the result is Array ( [0] => main_page=index [1] => cPath=70_229_242_240 )
Then print_r the second part
$pid = explode('=',$qs[0]);
gives a result of index
This is incorrect.
A simplified version of the code using the results is
if($pid[1] == '70_229_242_240'){
echo " true";
}
So you can see that what I actually need to have stored in $pid is taken from cPath=70_229_242_240
I've tried to change the explode to give me this data but i can't get the correct result. TBH, they always confuse the hell out of me.
RESOLVED.
Used $pid = explode('=',$qs[1]);
You can possibly change this code into:
$qs = explode('&',$_SERVER['QUERY_STRING']);
$pid = explode('=',$qs[0]);
to get rid of this red mark.
But in fact Dreamweaver is not a PHP editor, you should simple choose something else to write your PHP code.
you need to pass a string to explode() so try with implode() use limit -1 to get first array of explode
$pid = explode('=',implode('=',explode('&',$_SERVER['QUERY_STRING'],-1)));
else you need to two statement
$fr = explode('&',$_SERVER['QUERY_STRING']);
$pid = explode('=',$fr[0]);
Dreamweaver does'n recognise PHP 5.4, and as Michael stated, this expression is only correct since version 5.4
Related
I got used to this notation for creating empty arrays and add named elements to them when needed;
$array = [];
// in case there is an error
$array["error"][] = "new error message as element 0 of $array['error']";
Now I learned that the [] notation for arrays does not work in older versions of PHP, like PHP 5.2.
Instead I have to do;
$array = array(
"error" => array()
);
array_push($array["error"], "new error message as element 0 of $array['error']");
This way is a little bit inconvenient in my case because the great thing about the first code snippet is that the "error" entry in $array is only created when there is an actual error, whereas in the latter case the entry (although empty) exists either way.
Is there a way to get similar 'functionality' (i.e. specifying/adding named elements when needed, not at initialisation) in a way that is also easily readable in PHP 5.2?
EDIT:
The first code snippet in the original post was reading $array = array[];. The author corrected it after I posted this answer.
The first code snipped is incorrect. There is no such thing as array[]. The correct syntax is array().
$array = array();
// in case there is an error
$array["error"][] = "new error message as element 0 of $array['error']";
You don't have to worry about PHP versions. This syntax always worked on PHP since its dawn and it will probably work forever. Keep using it.
The first way of creating array in PHP is incorrect. This syntax works in PHP5.2 below too, so you dont need to worry about it. You don't need to use array_push and simply do following.
The correct syntax is:
$array = array(); // notice it doesn't to array[]
// add error when there is one
$array["error"][] = "new error message as element 0 of $array['error']";
So I am working with PHP to pass a PHP array over a jQuery Ajax request to another PHP page. This is quite the task. For some reason, my json_encode is returning an array instead of a string, I am not quite sure why. Here is my PHP code:
$new_spreadsheet = nl2br($_POST['spreadsheet']);
$new_spreadsheet = explode('<br />', $new_spreadsheet);
array_shift($new_spreadsheet);
$new_spreadsheet = array_values($new_spreadsheet);
echo json_encode($new_spreadsheet);
I would show you the output, but it is really long. Basically this is outputting a PHP array which consists of each row on the spreadsheet. This is what I want to have, but the problem is that I don't want the quotes and everything in the array. I am pretty sure I need to run json_decode, but when I do that my code returns an error saying that the parameter needs to be a string. I can tell something is not right, but am not quite sure what I need to change. I would appreciate any advice.
Update: At this point, when I try to loop through the array and print each value, the first array index is equal to a double quote like so: ". There are double quotes in random values throughout the area. I am not quite sure about what is causing this.
If I echo the rows from within the json_encoded PHP array onto the console, I get the following output:
"
correct value
correct value
correct value
"
You're using JSON, which means you have to adhere to somewhat more stringent syntax rules than Javascript's. e.g.
<?php
$arr = array('This' => 'is', 'an' => 'array in php');
echo json_encode($array);
?>
output:
{"This":"is","an":"array in PHP"}
There is NO way to avoid getting quotes on the values, as they're a fundamental requirement of JSON (and Javascript). If you don't want quotes, then don't use JSON.
try only br.
$new_spreadsheet = explode("<br>", $new_spreadsheet);
It will work. and json_enode can never return an array. try var_dump and check.Also make sure before you post, use htmlspecialcharacters.
I have this array:
$array = array();
$array['123'] = 'abc';
$array['456'] = 'def';
Now I would like to get data from that array based on a variable. This is what I tried:
$variable = '123';
$result = $array[$variable];
echo $result;
It appears to be wrong, but i don't know why. It results in a warning:
Illegal offset type […]
I ran that exact code into my compiler and it worked; possibly it is a white-space error (random characters you cant see but still cause bugs). I would try to physically retype that section of code and delete the old one.
I would suggest trying this to make sure the variable is cast as a string:
$result = $array[(string)$variable];
That's most likely your problem. I think maybe $post['id'] is either mistakenly a multi-dimensional array or somehow becoming an object of a type not accepted as an array key.
I've got this problem when trying to construct a variable name
(which should output a corresponding array element.)
I start with setting the variables for testing purposes, normally the piece variable would hold the first element of an array $piece.
$wordnumber = 0;
$piece[0] = "forever";
I later echo them right before my problem to see if they're still ok.
echo "$piece[0]";
echo "$wordnumber";
The output is ok.
forever0
But then comes the problem, as I'm trying to make a function that automatically handles every single array element, so I want it to construct the next corresponding variable every time. However somehow it has no value after construction.
$name = ${'piece[' . $wordnumber . ']'};
echo "$name";
outputs nothing...
I've tried a lot of different formatting, I really don't know why I'm failing so hard here.
The code isn't part of any function right now btw.
Update:
$name = $piece[$wordnumber] solves the problem
I'm curious though why my previous formatting didn't work as expected.
Update: Question solved by André, the problem was that $piece[0] wasn't actually part of an array. So $piece was the actual variable. After storing an actual array $piece = array("Redish", "Yellow", "Green"); at start and using global $piece; in my function everything started working like a charm.
I haven’t tested the code, but I think your first approach didn’t work because your variable’s name is “piece”, not “piece[0]”. In other words, ${'piece'}[0] should work, but ${'piece[0]'} is wrong. Try add this in the very beginning of your script and PHP should display you some complaints:
error_reporting(E_ALL);
ini_set("display_errors", 1);
Try just $name = $piece[$wordnumber]; and echo $name
this will output "forever"
How come when I echo $p, the variable which Im trying to fetch using this loop doesnt get displayed in the path.
$name_image2="picture.jpg";
for ($i=2; $i<=$nr_of_pics; $i++){
$img='name_image'.$i;
echo $$img; gives me this: 'picture.jpg' which is correct.
but when echoing $p like this:
$p="/SV/main/temp_images/$$img"; echo $p;
I get this: SV/main/temp_images/name_image2 --> the variable 'name_image2' doesnt get called here, why?
I want it to say: SV/main/temp_images/picture.jpg
Thanks
$p = "/SV/main/temp_images/" . $$img;
Ought to fix it.
Also, I would recommend learning how to use arrays. They are a much better way to have a set of data instead of variable variables.
Try $p="/SV/main/temp_images/{${$img}}";
When PHP is parsing the string and comes to a $, it looks at the next character to see if it makes a valid variable name. If not, it moves on. In this case, that means that the second $ is correctly interpreted, but the first one has already been passed by. The answer is to enclose the inner expression in brackets, so that it will be parsed before the outer one is.