Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6993413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:45:41+00:00 2026-05-27T19:45:41+00:00

I have a php ternary operator as follows that does not work: var newLoginTxt

  • 0

I have a php ternary operator as follows that does not work:

 var newLoginTxt = <?=$_SESSION['loginStatus']? 'Logged in' : 'Login'?>;
 alert("About to set the navbar's login text to: " + newLoginTxt);

No alert box appears at all.

However this does work:

var curLoginStatusSessionVar = <?=$_SESSION['loginStatus']? 'true' : 'false'?>;
alert("Testing: " + curLoginStatusSessionVar);

The alert box appears and says “Testing: false”

I have the following article seeming to say that the ternary operator in php CAN in fact return values other than ‘true’ or ‘false’:
http://davidwalsh.name/php-shorthand-if-else-ternary-operators

Okay, even more odd — in the code below, why does the first “Testing” alert box appear, then the 2nd “Testing” alert box NOT appear? It’s the exact same code:

      // THIS "Testing" ALERT BOX APPEARS FINE**
var curLoginStatusSessionVar = <?=$_SESSION['loginStatus']? 'true' : "false"?>;
alert("Testing: " + curLoginStatusSessionVar);


var newLoginTxt = <?=$_SESSION['loginStatus']? 'Logged in' : 'Login'?>;
alert("About to set the navbar's login text to: " + newLoginTxt);


    // THIS IDENTICAL "Testing" ALERT BOX NEVER APPEARS**
curLoginStatusSessionVar = <?=$_SESSION['loginStatus']? 'true' : 'false'?>;
alert("Testing" + curLoginStatusSessionVar);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:45:42+00:00Added an answer on May 27, 2026 at 7:45 pm

    Your problem is completely unrelated to the ternary expression: In JavaScript, like in modern php, strings must be encapsulated by quotes.

    true and false happen to be JavaScript constants. Therefore, you want:

    var newLoginTxt = "<?=$_SESSION['loginStatus']? 'Logged in' : 'Login'?>";
    //                ^                                                    ^
    

    Alternatively, and if the data is more complicated, use json_encode. For example, if you want to access the value of a php variable $title (which could contain quotes) in JavaScript:

    <?php $title = '"Java"Script'; ?>
    var title = <?php echo json_encode($title); ?>;
    alert(title);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have php scripts that do things like register, login, upload. I would like
I am fairly capable at using the PHP ternary operator. However I have hit
I have php code that doesn't work anymore $doc = DOMDocument::loadXML($string); $doc->formatOutput = true;
Is there a ternary operator or the like in PHP that acts like ??
I have PHP configured so that magic quotes are on and register globals are
We have PHP 5.2.6 deployed to c:\php and in that folder there is the
I have php script that creates a temporary watermark image for users that are
I have PHP scrip that goes like this: if ($cost_frm < $cost){ echo <script
Is it possible to have php not to require the begin/end tags ( <?php
In my live site I have php include() and require() that have full path

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.