• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 520
  • Last Modified:

How to ignore _ character, for use with php variables

$lab='HSLAB';
$room="102";
$computers="36";

I need to combine these to form HSLAB_102_36. $lab_$room_$comptuers does not combine them...if I put a space before the _ then it will print it...with the spaces in place, and i cannot have the spaces in place though. Is there some way to do an ignore next character command such as / or \ ? I tried both of those, and it does not work. My actual code sample is below, i need to somehow get it to output exactly "HSLAB_102_36"

$sqlquery = "select * from log where computer='$lab_$room_$computers' order by date DESC, time DESC;";
0
atkfrg56
Asked:
atkfrg56
  • 4
  • 2
  • 2
  • +2
2 Solutions
 
Nick_72Commented:
Hi, you could try this:

$sqlquery = "select * from log where computer='" . $lab . "_" . $room . "_" . $computers . "' order by date DESC, time DESC;";
0
 
Nick_72Commented:
Hmm, and the semi-colon after DESC should be removed I believe...
0
 
Diablo84Commented:
The underscore is recognised as a valid variable name character which is why it's not working, to avoid having to concatenate the variables you could instead using curly brackets, eg:

$sqlquery = "SELECT * FROM log WHERE computer='{$lab}_{$room}_{$computers}' ORDER BY date DESC, time DESC";

Diablo84
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
snoyes_jwCommented:
Both answers above are valid.  Here's a third, just because it can be done:
$sqlquery = sprintf("SELECT * FROM log WHERE computer='%s_%s_%s' ORDER BY date DESC, time DESC", $lab, $room, $computers);
0
 
atkfrg56Author Commented:
Thanks for your very quick posts, I have found that 2 of the 3 methods posted work perfectly and will split the points.
0
 
atkfrg56Author Commented:
The semi colon can be left in or left out - same results. I usually keep it in because its needed if i do a manual query right on the server. Thanks again.
0
 
ZhaawZSoftware DeveloperCommented:
$sqlquery = "select * from log where computer='{$lab}_{$room}_{$computers}' order by date DESC, time DESC;";

It's called 'heredoc syntax'. Read section 'Strings' in PHP manual, if more info is needed.
This syntax is also great when using multidimensional arrays.
0
 
atkfrg56Author Commented:
sorry ZhaawZ, but  Diablo84 already posted that snippet of code 30 minutes before you, and you posted 10 minutes after I closed this question and awarded points
0
 
ZhaawZSoftware DeveloperCommented:
didn't have a time to read all answers, therefore didn't see that someone has already posted about heredoc syntax...
0
 
snoyes_jwCommented:
Actually, the form using curly brackets is called "complex" syntax.  heredoc uses <<<, like

$sqlquery = <<<SQL
SELECT * FROM log WHERE computer = $lab
SQL;
0
 
atkfrg56Author Commented:
snoyes_jw,
I could not get your code snippet to work on the windows php, but it works great on the linux php system. Sorry, but i should have awarded points to you also.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now