473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

reading variables in a filename

I believe this is a simple problem but I'm not able to figure it out. I have a file that has a filename on each line. I read through this file setting the string to a variable. Then I am trying to use this variable in opening the file but it won't read it. This is my current code.
Expand|Select|Wrap|Line Numbers
  1. while (! feof($ts_proj)){
  2.         $filename= fgets($ts_proj);
  3.                 $codes=fopen('/projects/$filename/ts_codes',"r");
  4. }
  5.  
The error says it cant open /project/$filename/ts_codes instead of /project/project1/tscodes which means the variable is passing through. Any help is appreciated.
Jun 22 '09 #1
6 1514
oh..okay..i figured it out. I forgot to add the "." in before and after my variable.=)
Jun 22 '09 #2
Markus
6,050 Expert 4TB
@phpnewbie26
Single quotations will not parse the string for variables. Double quotes, however, will.

Expand|Select|Wrap|Line Numbers
  1. $name = "Mark";
  2. echo "My name is $name.";
  3. // Output: My name is Mark.
  4.  
For readability's sake, if you use this technique, wrap the variable in curly braces.
Expand|Select|Wrap|Line Numbers
  1. $name = "Mark";
  2. echo "My name is {$name}.";
  3.  
Jun 22 '09 #3
dlite922
1,584 Expert 1GB
besides better readability I think the curly braces were used in cases where your variable is not surrounded by space.

e.g. : this won't work:
echo "6584564$someNum2345";

solution:
echo "6584564${someNum}2345";

:)

Cheers,


Dan
Jun 22 '09 #4
Atli
5,058 Expert 4TB
@dlite922
Interesting. I didn't know putting the $ outside the braces worked to.

I always do
Expand|Select|Wrap|Line Numbers
  1. echo "6584564{$someNum}2345";
But they both seem to work :D

I wonder if there are any differences...
* Goes for the Google bookmark *
Jun 22 '09 #5
Dormilich
8,658 Expert Mod 8TB
@Atli
there are some edge cases, mostly concerning dynamic variables and dynamic function calls.
Expand|Select|Wrap|Line Numbers
  1. $$array[1] vs. ${$array[1]}
  2. $obj->{$method}()
Jun 22 '09 #6
thanks guy! This is all very helpful!=)
Jun 22 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Dave | last post by:
Hi folks, I seem to be using these newsgroups a good bit and probabely will be for the next three or so months. I wonder if there is a workaround to a problem I'm having. PHP always says that...
7
by: Daniel Moree | last post by:
I'm working on a program that must first establish if the file exists in the program directory then it must open if for reading, read each line and set the variables then the program goes on about...
6
by: GG | last post by:
Is this public static method thread safe. //Receives a file name as a parameter //and returns the contents of that file as a string public static string FileToStr(string fileName) { FileStream...
4
by: News | last post by:
Hi Everyone, The attached code creates client connections to websphere queue managers and then processes an inquiry against them. The program functions when it gets options from the command...
7
by: jccorreu | last post by:
I've got to read info from multiple files that will be given to me. I know the format and what the data is. The thing is each time we run the program we may be using a differnt number of files,...
1
by: Andrea Gavana | last post by:
Hello NG, that may sound a silly question, but I didn't find anything really clear about the issue of reading unformatted big endian files with Python. What I was doing till now, was using...
4
waynetheengineer
by: waynetheengineer | last post by:
Hello everyone :) I am trying to write VB code for reading filenames and file property values in a specific directory. For example, I have a directory called C:/Bears and in that directory...
5
by: ycquak | last post by:
Hi, Currently I need to read contents from a dynamic page, which goes something like: http://servername/filename.jsp?arg1=a&arg2=b The content is not static, neither is there a file for me to...
0
Guido Geurs
by: Guido Geurs | last post by:
I'm writing a program that list the contents of a CDrom and also the contents of the ZIP files. When there is a bad Zip file on the CD, the program keeps traying to reed the file and after +- 50...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.