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
PHP Code so far.
echo $sender = isset($_GET['s']) ? $_GET['s'] : "null";
echo $receiver = isset($_GET['r']) ? $_GET['r'] : "null";
echo $timestamp = isset($_GET['t']) ? $_GET['t'] : "0";
echo "<br/>";
echo $sTotalItems = isset($_GET['si']) ? intval($_GET['si']) : 0;
echo $rTotalItems = isset($_GET['ri']) ? intval($_GET['ri']) : 0;
echo "<br/>";
for ($i = 0; $i < $sTotalItems; $i++) {
echo $input = isset($_GET['si'+$i]) ? urldecode($_GET['si'+$i]) : "null";
if ($input == "null")
continue;
$input = explode(":", $input);
var_dump($input);
}
What I'm trying to do is dynamically grab a GET Variable. I'm sending multiple GET requests, and they all contain basically the same data, just small differences. - My question is simple though. This doesn't work like I'm thinkin it should in my mind.
$_GET['si'+$i];
In my mind, this should turn into..
$_GET['si1'];
Can this be done? Or am I going to have to figure out another way to do this?
What this is doing.. is I'm sending multiple requests.. in the following order basically..
http://dummy.com/integrate.php?s=me&r=you&t=3425&si=1&ri=2&si0=item:1:2&ri0=item:2:1&ri1=item:3:4
I'm trying to make it dynamically possible, to send more than one "item," in which there is an identifier telling the system, how many items there are for each sender, and receiver. Then a loop goes through each sender & receivers' items, and then separates the item, into 3 values. the name, the id, and the amount. The problem, is the code, isn't grabbing the item at all. Am I correct, to assume, you can't use the $_GET method, and a variable together?
Yes you can use dynamic variables along with $_GET, the only problem is use . (period) for concatenation not +.
PHP is loosely typed, so yes, you can "add" a number to a string and the number will be converted to its string representation for you. You use the concatenation operator (.) to do that, not the addition operator (+). Use $_GET['si'.$i].
The other answers show how to do what you are trying, however it's better to just use arrays.
Use item[]=something&item[]=else or even item[1]=something&item[2]=else.
Then you can access $_GET['item'][0] or $_GET['item'][1], etc.
Related
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 4 years ago.
Improve this question
I am having some problems with my program.
I am creating a script which displays all the properties in the properties table in our database and one row in the table should show available or sold. it should check if the puserId is null or 1 or more
//Execute the Query
$records = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($records)){
echo "<tr>";
echo "<td>".$row['propertyId']."</td>";
echo "<td>".$row['propNum']."</td>";
echo "<td>".$row['AptNum']."</td>";
echo "<td>".$row['street']."</td>";
if (empty(['puserId'])) {
$status = 'Available';
}else {
$status = 'Sold';
}
echo "<td>".$status."</td>";
When i use this it shows that all properties are sold, also the ones wich have a puserId of null. It doesn't give me any errors though.
Anybody knows how I should do this?
Thanks in advance :)
Replace:
if (empty(['puserId'])) {
with:
if (empty($row['puserId'])) {
in order to fix the missing variable $row typo (your actual code checks whether the array ['puserId'] is null, which is obviously false) . If your field is either null or numeric, you can also write that as:
if (is_null($row['puserId'])) {
but my advice is to avoid using empty/null values in table fields related to identifiers. Just give them a default value of 0 to make everything easier. At that point you could write your check as:
if ($row['puserId'] == 0) {
Other than the $row['puserId'] typo, empty doesn't really look useful there. It includes an isset check that is pointless considering you know that column exists. You aren't checking that any of the other columns are set before you use them. There's really no need to check that one either. Just evaluate it directly. null or 0 values will evaluate as false in the if condition..
if ($row['puserId']) {
$status = 'Sold';
} else {
$status = 'Available';
}
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 a beginner in php , and i want make a test with GET .
I want display , for example "ok" on my php page if my id parameter is 1 but i have always 1 when i change the id parameter to another value .
Details :
when i make this url :
http://localhost:81/test/testajax.php?id=2
expected result :
not ok
obtained result :
ok
testajax.php
<?php
if($_GET["id"] = 1)
{
die('ok');
}
else
{
die('not ok');
}
?>
One equal sign (=) sets the value of a variable. $foo = "bar"; would set $foo to store bar.
You want to use two equal signs (==), which is a comparison operator. ($foo == "bar") would check to see if $foo is equal to bar.
You can check the different types of operators at http://php.net/manual/en/language.operators.php
May be you should go through this basics before you start.
you should put two equal signs to compare.
if($_GET["id"]==1)
correct code:
<?php
if($_GET["id"] == 1)
{
die('ok');
}
else
{
die('not ok');
}
?>
As you are setting the variable, the if statement is always equating to true
Just thought it was worth Noting this as that is the logical reason for your issue
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 think this is simple question , I am having trouble in passing a variable with a . in the name for example
www.example.com/index.php?auth.start=facebook
if I change this url to
www.example.com/index.php?authstart=facebook
and I try to get the variable like this
$var = $_GET[authstart];
echo $var;
It works fine , But
$var = $_GET[auth.start];
echo $var;
this shows no value in $var can any one help me out with this
thanks in Advance
try with $_GET["auth_start"]
Hope that helps :)
A . in a variable name is not valid in PHP, so with $_GET PHP will convert the . to a _. It does this because register_globals will extract the $_GET array into individual PHP variables which can't contain the ..
Doing print_r($_GET) will show you this.
User defined arrays work fine: $a['x.y'] = 1 etc...
Array keys are strings. While php may forgive you sometimes for omitting them, they should be enclosed. so always do
$_GET['yourkey'];
and never
$_GET[yourkey];
in your case, this is the correct syntax:
$var = $_GET['auth.start'];
But then, as #AbraCadaver says, that will also not work, for you . is changed to a _.
So use
$var = $_GET['auth_start'];
To go a bit deeper: if you don't actually make it a string, you'll get this notice:
PHP Notice: Use of undefined constant yourkey - assumed 'yourkey'
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Is it better (safer/more readable) to use list or to assign values one after another ?
Examples :
With list :
$head = '';
if(strpos($data, "\r\n") !== false)
list($status, $head) = explode("\r\n", $data, 2);
else
$status = $data;
Without list :
$head = '';
$components = explode("\r\n", $data, 2);
$status = $components[0];
if(count($components) === 2) //Or isset($components[1])
$head = $components[1];
What's the preferred way to write this piece of code ?
Depending on the use case list may end up causing you problems. Conventionally defining the values however would not.
Notes from http://PHP.net:
list() only works on numerical arrays and assumes the numerical indices start at 0.
.
Modification of the array during list() execution (e.g. using list($a, $b) = $b) results in undefined behavior.
.
. . .if you are using arrays with indices you usually expect the order of the indices in the array the same you wrote in the list() from left to right; which it isn't. It's assigned in the reverse order.
Clearly using list can cause some unexpected behavior, and it definitely isn't the most common way to do this. This makes list bad for readability because it's unconventional.
As far as I know there are no benefits to using list , and I would assume that it is slower.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
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.
Improve this question
<br />↵<b>Parse error</b>: syntax error, unexpected ':' in <b>.../ajax.php</b> on line <b>87</b>
LINE 87: $conditions = ($this->input->post()) ? : array('tutor'=>$this->session->userdata('user_id'));
line 87 works fine on localhost, but when I use godaddy I get that error. Is there something I need to set in php.ini or something to get Ternary operators to work?
Thanks!
The ternary operator (as the name suggests) usually expects 3 arguments
$var = $expr ? $trueValue : $falseValue;
With PHP5.3 its allowed to omit $trueValue. In this case its $expr is used for it
$var = $expr ? : $falseValue;
// same as
$var = $expr ? $expr : $falseValue;
You probably don't have PHP5.3 on your server. As you can see in my example its quite easy to fix this and make it ready for pre-5.3
$conditions = ($this->input->post())
? ($this->input->post())
: array('tutor'=>$this->session->userdata('user_id'));
The syntax ? : you are using is PHP 5.3 only.
Set a default value:
$conditions = ($this->input->post()) ? $this->input->post() : array('tutor'=>$this->session->userdata('user_id'));
The accepted answer is correct, but misses one important point:
$x = $a ? : $b; // valid in PHP 5.3
should indeed be replaced with
$x = $a ? $a : $b; // valid in older versions of PHP
However, when you're dealing with functions, not variables, be aware of side-effects:
$conditions = ($this->input->post())
? ($this->input->post())
: array('tutor'=>$this->session->userdata('user_id'));
In the above case, if $this->input->post()returns a truthy value, the function will be executed again, which may not be what you want. You can see this more clearly by expanding the ternary operator to its full form:
if ($this->input->post()) {
$conditions = $this->input->post();
} else {
$conditions = array('tutor' => $this->session->userdata('user_id'));
}
You can see that the function is executed on lines one and two above. If you don't want that, try this instead:
if (!$conditions = $this->input->post()) {
// Single equal sign in an if condition: make assignment, and check
// whether the result is truthy.
$conditions = array('tutor' => $this->session->userdata('user_id'));
}
This is a fairly common pattern in my own code. This will execute the function $this->input->post() only once. If the result is truthy, that result is stored in $conditions. If the result is not truthy, the code inside the if condition is run. This assigns the fallback value to $conditions. The benefit is that, in either case, $this->input->post() is run only once.