?
Solved

How to ignore _ character, for use with php variables

Posted on 2005-05-11
11
Medium Priority
?
516 Views
Last Modified: 2008-02-26
$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
Comment
Question by:atkfrg56
  • 4
  • 2
  • 2
  • +2
11 Comments
 
LVL 9

Accepted Solution

by:
Nick_72 earned 1000 total points
ID: 13977304
Hi, you could try this:

$sqlquery = "select * from log where computer='" . $lab . "_" . $room . "_" . $computers . "' order by date DESC, time DESC;";
0
 
LVL 9

Expert Comment

by:Nick_72
ID: 13977316
Hmm, and the semi-colon after DESC should be removed I believe...
0
 
LVL 27

Assisted Solution

by:Diablo84
Diablo84 earned 1000 total points
ID: 13977375
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 33

Expert Comment

by:snoyes_jw
ID: 13977428
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
 
LVL 2

Author Comment

by:atkfrg56
ID: 13977549
Thanks for your very quick posts, I have found that 2 of the 3 methods posted work perfectly and will split the points.
0
 
LVL 2

Author Comment

by:atkfrg56
ID: 13977561
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
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 13977692
$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
 
LVL 2

Author Comment

by:atkfrg56
ID: 13977951
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
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 13979214
didn't have a time to read all answers, therefore didn't see that someone has already posted about heredoc syntax...
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 13982207
Actually, the form using curly brackets is called "complex" syntax.  heredoc uses <<<, like

$sqlquery = <<<SQL
SELECT * FROM log WHERE computer = $lab
SQL;
0
 
LVL 2

Author Comment

by:atkfrg56
ID: 13984989
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

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month15 days, 3 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question