0

I am attempting some, what I thought basic, string concatenation with a variable. My issue is that the variable is never displayed on screen, but I know it is assigned as if I echo the results to screen it displays as it should. This is my syntax:

var year = datestring.substring(0,4);
if (<$php echo $salesqtr; ?> == "first") { var labelsarr = ["Jan " + year, "Feb " + year, "Mar " + year]; }

What is the proper way to append the value of year to the month in the string above?

Smith Stanley
  • 461
  • 1
  • 8
  • 25

1 Answers1

1

If you're echoing a string, it has to be in quotes

var year = datestring.substring(0,4);

if ("<?php echo $salesqtr; ?>" == "first") { 
    var labelsarr = ["Jan " + year, "Feb " + year, "Mar " + year]; 
}

Generally it's a better idea to keep javascript in .js files, and echo values in data attributes in the HTML, and then just get them with javascript.

Also, PHP start and stop tags use a questionmark

<?php ...  ?>

or when echoing you can just use short tags, which are always available in later versions of PHP

<?= $salesqtr; ?>
adeneo
  • 312,895
  • 29
  • 395
  • 388