6 Replies - 417 Views - Last Post: 02 October 2012 - 06:34 AM Rate Topic: -----

#1 nightscream  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 237
  • Joined: 04-December 08

server problems with ?: (quick ternary)

Posted 02 October 2012 - 05:19 AM

FIXED

Hi I have my own dev server which is running php 5.4.7

Quote

PHP 5.4.7 (cli) (built: Oct 2 2012 12:10:57)
Copyright © 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright © 1998-2012 Zend Technologies
with Xdebug v2.2.1, Copyright © 2002-2012, by Derick Rethans


The problem is ?: is giving me a weird http error once in a while (more often than you think)
http://dev.coding-tech.com/test.php
http://dev.coding-tech.com/test2.php

test.php
<?php

$test = "xxx";

echo ($test ?: "no value");

$x = null;
echo "<br />" . ($x ?: "no value");



test2.php
<?php

$test = "xxx";

echo ($test ? $test : "no value");

$x = null;
echo "<br />" . ($x ? $x : "no value");


This post has been edited by nightscream: 02 October 2012 - 06:35 AM


Is This A Good Question/Topic? 0
  • +

Replies To: server problems with ?: (quick ternary)

#2 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,297
  • Joined: 22-October 08

Re: server problems with ?: (quick ternary)

Posted 02 October 2012 - 05:35 AM

That's because the variable on the right hand side of ? requires a variable with a value, even if the value is null.

I found this in the PHP manual:

Ternary

Hope this helps :)

This post has been edited by aaron1178: 02 October 2012 - 05:36 AM

Was This Post Helpful? 0
  • +
  • -

#3 nightscream  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 237
  • Joined: 04-December 08

Re: server problems with ?: (quick ternary)

Posted 02 October 2012 - 05:47 AM

Huh no ?: should work from version 5.3

in test1.php
I should get xxx
no value
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: server problems with ?: (quick ternary)

Posted 02 October 2012 - 06:11 AM

Where do you see anywhere that this

echo ($test ?: "no value");


should work?


EDIT: Oh wait, I found it!

Quote

Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.


That's new to me. Sorry.

Works for me in 5.3.15:
<?php

$test = "xxx";

echo ($test ?: "no value");

$x = null;
echo "<br />" . ($x ?: "no value");




php -f tern.php 
xxx<br />no value


AND it looks like what you get as well?

This post has been edited by JackOfAllTrades: 02 October 2012 - 06:16 AM

Was This Post Helpful? 1
  • +
  • -

#5 nightscream  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 237
  • Joined: 04-December 08

Re: server problems with ?: (quick ternary)

Posted 02 October 2012 - 06:13 AM

Lol everywhere. Shortened ternary operator it is called and working from php 5.3
It's in some big frameworks like symfony and zend.

I got it to work, it was a problem with apc and fixed now by updating.

@4 JackOfAllTrades
No problem, thanks for the help anyway :)

This post has been edited by nightscream: 02 October 2012 - 06:14 AM

Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3486
  • View blog
  • Posts: 10,055
  • Joined: 08-June 10

Re: server problems with ?: (quick ternary)

Posted 02 October 2012 - 06:22 AM

using your links I didnít see any error ever.
Was This Post Helpful? 0
  • +
  • -

#7 nightscream  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 237
  • Joined: 04-December 08

Re: server problems with ?: (quick ternary)

Posted 02 October 2012 - 06:34 AM

View PostDormilich, on 02 October 2012 - 07:22 AM, said:

using your links I didnít see any error ever.

Hehe, I fixed it, it was a server problem. I thought it was maybe something else that's why I posted. I looked at the problem for a day and couldn't find anything that's why I posted.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1