Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
We want to connect the people who have knowledge to the people who need it, to bring together people with different perspectives so they can understand each other better, and to empower everyone to share their knowledge.
Mila Oberoi
I use this code to get the full URL:
$actual_link = ‘http://’.$_SERVER[‘HTTP_HOST’].$_SERVER[‘PHP_SELF’];
The problem is that I use some masks in my .htaccess, so what we see in the URL is not always the real path of the file.
What I need is to get the URL, what is written in the URL, nothing more and nothing less—the full URL.
I need to get how it appears in the Navigation Bar in the web browser, and not the real path of the file on the server.
Mila Oberoi
I’m trying to turn MySQLi query errors to Exceptions, but couldn’t – mysqli_sql_exception is thrown only if it failed to connect the DB.
I used mysqli_report(MYSQLI_REPORT_STRICT) and procedural MySQLi functions embedded to custom wrapper class.
Former code:
public function mysqlQuery($SQL) {
$this->Result = mysqli_query($this->DBlink, $SQL);
if($this->Result === false)
throw new MySQLiQueryException($SQL, mysqli_error($this->DBlink), mysqli_errno($this->DBlink));
return $this->Result;
}
Question: Is it normal no Warning, nor Exception are thrown when query fails so I have to check if mysqli_query() returned false?
Mila Oberoi
I have a web application on a Linux server which starts with <?
I needed to copy this application to a windows environment and everything is working fine except that an SQL statement is being rendered differently. I don't know if this has to do with the script beginning with <?php instead of <? because I don't know from where to enable the <? from the PHP.ini so I changed it to <?php
I know that these 2 statements are supposed to mean the same but I need to test it with <? in order to ensure that the application is exactly the same. This way I can eliminate another possibility.
Thanks
Mila Oberoi
Trying to find the links on a page.
my regex is:
/]*href=("'??)([^"' >]*?)[^>]*>(.*)/
but seems to fail at
what?
How would I change my regex to deal with href not placed first in the a (anchor) tag?
Mila Oberoi
Take a look here: http://longgoldenears.blogspot.com/2007/09/triple-equals-in-javascript.html
The 3 equal signs mean “equality without type coercion”. Using the triple equals, the values must be equal in type as well.
0 == false // true
0 === false // false, because they are of a different type
1 == “1” // true, automatic type conversion for value only
1 === “1” // false, because they are of a different type
null == undefined // true
null === undefined // false
‘0’ == false // true
‘0’ === false // false
Mila Oberoi
Let me prefix this by saying that I know what foreach is, does and how to use it. This question concerns how it works under the bonnet, and I don’t want any answers along the lines of “this is how you loop an array with foreach”.
For a long time I assumed that foreach worked with the array itself. Then I found many references to the fact that it works with a copy of the array, and I have since assumed this to be the end of the story. But I recently got into a discussion on the matter, and after a little experimentation found that this was not in fact 100% true.
Let me show what I mean. For the following test cases, we will be working with the following array:
$array = array(1, 2, 3, 4, 5);
Test case 1:
foreach ($array as $item) {
echo “$item\n”;
$array[] = $item;
}
print_r($array);
/* Output in loop: 1 2 3 4 5
$array after loop: 1 2 3 4 5 1 2 3 4 5 */
This clearly shows that we are not working directly with the source array – otherwise the loop would continue forever, since we are constantly pushing items onto the array during the loop. But just to be sure this is the case:
Test case 2:
foreach ($array as $key => $item) {
$array[$key + 1] = $item + 2;
echo “$item\n”;
}
print_r($array);
/* Output in loop: 1 2 3 4 5
$array after loop: 1 3 4 5 6 7 */
This backs up our initial conclusion, we are working with a copy of the source array during the loop, otherwise we would see the modified values during the loop. But…
If we look in the manual, we find this statement:
When foreach first starts executing, the internal array pointer is automatically reset to the first element of the array.
Right… this seems to suggest that foreach relies on the array pointer of the source array. But we’ve just proved that we’re not working with the source array, right? Well, not entirely.
Test case 3:
// Move the array pointer on one to make sure it doesn’t affect the loop
var_dump(each($array));
foreach ($array as $item) {
echo “$item\n”;
}
var_dump(each($array));
/* Output
array(4) {
[1]=>
int(1)
[“value”]=>
int(1)
[0]=>
int(0)
[“key”]=>
int(0)
}
1
2
3
4
5
bool(false)
*/
So, despite the fact that we are not working directly with the source array, we are working directly with the source array pointer – the fact that the pointer is at the end of the array at the end of the loop shows this. Except this can’t be true – if it was, then test case 1 would loop forever.
The PHP manual also states:
As foreach relies on the internal array pointer changing it within the loop may lead to unexpected behavior.
Well, let’s find out what that “unexpected behavior” is (technically, any behavior is unexpected since I no longer know what to expect).
Test case 4:
foreach ($array as $key => $item) {
echo “$item\n”;
each($array);
}
/* Output: 1 2 3 4 5 */
Test case 5:
foreach ($array as $key => $item) {
echo “$item\n”;
reset($array);
}
/* Output: 1 2 3 4 5 */
…nothing that unexpected there, in fact it seems to support the “copy of source” theory.
The Question
What is going on here? My C-fu is not good enough for me to able to extract a proper conclusion simply by looking at the PHP source code, I would appreciate it if someone could translate it into English for me.
It seems to me that foreach works with a copy of the array, but sets the array pointer of the source array to the end of the array after the loop.
Is this correct and the whole story?
If not, what is it really doing?
Is there any situation where using functions that adjust the array pointer (each(), reset() et al.) during a foreach could affect the outcome of the loop?
Mila Oberoi
Do getElementsByClassName (and similar functions like getElementsByTagName and querySelectorAll) work the same as getElementById or do they return an array of elements?
The reason I ask is because I am trying to change the style of all elements using getElementsByClassName. See below.
//doesn’t work
document.getElementsByClassName(‘myElement’).style.size = ‘100px’;
//works
document.getElementById(‘myIdElement’).style.size = ‘100px’;
Mila Oberoi
This question already has an answer here:
Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop 23 answers
In Java, is it legal to call remove on a collection when iterating through the collection using a foreach loop? For instance:
List names = ….
for (String name : names) {
// Do something
names.remove(name).
}
As an addendum, is it legal to remove items that have not been iterated over yet? For instance,
//Assume that the names list as duplicate entries
List names = ….
for (String name : names) {
// Do something
while (names.remove(name));
}