Error message: Fatal error: Can't use function return > value in write context in - php-5.3

I am trying to run some code from a book. There appears to be a problem with the code.
Here is the error message:
Fatal error: Can't use function return
value in write context in
/Applications/MAMP/htdocs/Eclipse-Workspace/simpleblog/test.php
on line 24
Here is the code referenced in the message (starting on line 24)
if (!empty(trim($_POST['username']))
&& !empty(trim($_POST['email']))) {
// Store escaped $_POST values in variables
$uname = htmlentities($_POST['username']);
$email = htmlentities($_POST['email']);
$_SESSION['username'] = $uname;
echo "Thanks for registering! <br />",
"Username: $uname <br />",
"Email: $email <br />";
}
I would appreciate any help. Please let me know if I need to provide any more information
Thanks a lot guys. That was very fast. The solution works great.
The problem is that the empty() function needs to be applied only to direct variables.
For future reference:
The code is from 'PHP for Absolute Beginners' by Jason Lengstorf (2009), pages 90-91, Chapter 3, $_SESSION
corrected code:
//new - Created a variable that can be passed to the empty() function
$trimusername = trim($_POST['username']);
//modified - applying the empty function correctly to the new variable
if (!empty($trimusername)
&& !empty($trimusername)) {
// Store escaped $_POST values in variables
$uname = htmlentities($_POST['username']);
$email = htmlentities($_POST['email']);
$_SESSION['username'] = $uname;
echo "Thanks for registering! <br />",
"Username: $uname <br />",
"Email: $email <br />";
}

In short: The empty() function only works directly on variables
<?php
empty($foo); // ok
empty(trim($foo)); // not ok
i'd say, for the course of getting further with that book, just use a temporary variable
so change:
if (!empty(trim($_POST['username']))
to
$username = trim($_POST['username']);
if(!empty($username)) {
//....

Exactly your example is mentioned at the manual
Note:
empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)).
Use a temporary variable, or just test against "empty string"
if (trim($foo) !== '') {
// Your code
}

Related

DOMDocument - How to get all inner text except from style/script tags?

I spent so much time on a very simple thing and had to post here on StackOverflow
I want to get all inner text except the script/style tags
$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
$html = <<<EOD
<div>
<script>var main=0</script>
<div>
<p>my</p>
<script>var inner=0</script>
</div>
<p>text</p>
only
</div>
EOD;
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
echo $entries = $xpath->query('//*[not(self::script)]')->item(0)->nodeValue;
gives me
var main=0 my var inner=0 text only
and also tried
$entries = $xpath->query('//*[not(self::script)]');
foreach ($entries as $entry) {
if ($entry->tagName == 'style' || $entry->tagName == 'script') {
continue;
}
echo preg_replace('/\s\s+/', ' ', $entry->nodeValue);
}
gives me
var main=0 my var inner=0 text only var main=0 my var inner=0 text only var main=0 my var inner=0 text only my var inner=0mytext
I tried several xpaths but it doesn't work
my desired output is my text only
I am a Scrapy developer and I do that easily in Scrapy, but having a bad time with PHP today
Unfortunately, PHP doesn't support xpath 2.0 (and, IIRC, neither does Scrapy), so the name() method which would have made it easy, isn't available...
The closest thing I can think of is the following, which should get you close enough (note that, because there is no <style> tag in your $html, I only focused on <script>):
$entries = $xpath->query('//*[not(./text()/parent::script)]/text()');
foreach ($entries as $entry) {
echo trim($entry->textContent) . " ";
}
Output:
my text only

Remove older $_GET from url

I've got some problems with my url on my website. I'm trying to get a link with the given GET parameters, but i'm getting my previous parameter aswell.
My url looks like this:
www.cdwinkel.dev/search-results?genre=Pop&medium=DVD&medium=Single .
It should be:
www.cdwinkel.dev/search-results?genre=Pop&medium=Single .
I'm running the following code:
$data['url'] = createurl();
function createurl(){
$i = 1;
$string = "?";
$keys = array_keys($_GET);
foreach($_GET as $get){
if($get != ""){
$string .= $keys[$i] . "=" . $get ."&";
$i++;
}
}
$string = rtrim($string, "&");
return $string;
}
$i = 1, because my first value in my array is empty.
And my button looks like this:
<a href='".$data['url'].'&medium='.$names[$i]."'>
I guess I should'nt set &medium=.$names[$i] in the href tag,
but I wont get the new $names[$i] in my function, so I won't get a new url if i wont add it in.
I'm looking forward to your responce.
Sincerely,
Kars Takens
At this point i've created an array with the right arraykeys and values.
$url = array_slice($_GET, 1);
This returns the following array:
array (size=2)
'genre' => string 'Pop' (length=3)
'medium' => string 'DVD' (length=3)
After this I decoded this into a new string:
genre=Pop&medium=DVD
I got 6 button which I created in a foreach loop, but i'm getting &medium='VALUE' Twice. This only happens after the first time. So the first time my button works well.
<?php
$names = array_keys($data['tellen']);
$i = 0;
foreach($data['tellen'] as $m){
echo "<li><a href='search-results?".$data['url'].'&medium='.$names[$i]."'>". $names[$i] ." <span class='product-amount'>(". $m[0]->count. ")</span></a></li>";
$i++;
}
Hopefully you can help me further with this information.
I solved my problem by adding this in my forloop:
foreach($data['tellen'] as $m){
if(isset($_GET['medium'])){
unset($_GET['medium']);
$url = array_slice($_GET, 1);
$data['url'] = urldecode(http_build_query($url));
}
echo "<li><a href='search-results?".$data['url'].'&medium='.$names[$i]."'>". $names[$i] ." <span class='product-amount'>(". $m[0]->count. ")</span></a></li>";
$i++;
}

Remove a specific character of plaintext with simple dom html

I have this code to retrieve some products on a website:
foreach($page1->find('.link_category') as $produit1)
echo $produit1->plaintext . '<br />';
I retrieve product with double quote ", i would like to replace it (the double quote)by nothing
thanks for help
foreach($page1->find('.link_category') as $produit1) {
$product = str_replace('"','',$produit1->plaintext);
echo $product;
}
I hope it works :)

Is is possible to have strings evaluate as variables in groovy?

I am playing with grails and groovy. I wondered if its possible to do something like this.
def inbuiltReqAttributes = ['actionName','actionUri','controllerName','controllerUri']
inbuiltReqAttributes.each() { print " ${it} = ? " };
what would i put in the ? to get groovy to evaluate the current iterator value as a variable e.g. to do it the long way
print " actionName = $actionName "
Thanks
I believe off the top of my head, this should work:
print " ${it} = ${this[ it ]}"
Or:
print " ${it} = ${getProperty( it )}"
But i'm not at a computer to 100% verify this atm...
Try this:
inbuiltReqAttributes.each() {
evaluate("value = ${it}")
print "$it = $value"
}

ob_get_clean and ob_get_contents return content to screen instead of putting it into variable when used with var_dump

I use this function for debugging:
function d($v,$tofile=null) {
static $wasused;
ob_start();
var_dump($v);
$dump = ob_get_clean();
if (is_array($v)) $dump = preg_replace("#=>\n#",'=>',$dump);
if (strlen($dump)>1000 or $tofile) {
fileput('debug.txt',$dump,$wasused);
echo n.n."strlen=".strlen($dump)." >> debug.txt".n.n;
}
elseif (strlen($dump)<80) echo $dump;
else echo n.n.$dump.n.n;
$wasused=true;
}
the problem is it sometimes return content to console, particularly when this content is var_dump result on a big array,
anyone of you have seen this problem before ?
If this is in your php.ini:
implicit_flush = On
change it to this:
implicit_flush = Off
Before assuming that there is a problem with var_dump itself, one would need to verify that fileput() does exactly what the question implies.

Resources