473,387 Members | 1,425 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,387 software developers and data experts.

How can I set some code to be a variable?

106 100+
I need help using quotes on PHP.

I have this code:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $myfile = "$xxx.txt";
  3. $work = fopen ($myfile, "r");
  4. $hits = fread ($work, filesize($myfile));
  5. fclose ($work);
  6. echo "xxxx: <b>$hits</b>";?>);
  7. $filenameb = "stats.php";
  8. $fdb = fopen ($filenameb, "a");
  9. fwrite ($fdb,$contentsb);
  10. fclose ($fdb);
  11. ?>
And what I want is to set all that code as a variable.
But I can't because of the double quotes! And if I use single quotes then the code won't work.

What I have is this:

Expand|Select|Wrap|Line Numbers
  1. $MYVARIABLE = "
  2.  
  3. <?php
  4. $myfile = "$xxx.txt";
  5. $work = fopen ($myfile, "r");
  6. $hits = fread ($work, filesize($myfile));
  7. fclose ($work);
  8. echo "xxxx: <b>$hits</b>";?>);
  9. $filenameb = "stats.php";
  10. $fdb = fopen ($filenameb, "a");
  11. fwrite ($fdb,$contentsb);
  12. fclose ($fdb);
  13. ?>
  14.  
  15. ";
You can see... I have plenty of double quoutes there...

Is there any way of doing this?
Jul 10 '10 #1

✓ answered by Samishii23

Try using file_get_contents()
It would be a little easier on the eyes.

7 1297
TheServant
1,168 Expert 1GB
Look at heredoc statements.

Also, read up on escaping strings with '\' and using addslashes.
Jul 10 '10 #2
londres9b
106 100+
Thank you so much!
Jul 10 '10 #3
TheServant
1,168 Expert 1GB
@londres9b
No worries, let us know how you go. I am curious to find out how you plan to use this variable?
Jul 10 '10 #4
londres9b
106 100+
Oh, it's for a URL shortener statistics.

That variable is going to be on the page that redirects the user and what it does (as you can see) is append to the statistics another code that opens (and reads) a txt file where the hits for that url have already been incremented.

Sounds confusing... probably there is a simpler way of doing this but I'm on a "make-my-own-way" kind of mood.. :)

When it's done (the Url Shortener) I'll tell you about it.
Jul 10 '10 #5
londres9b
106 100+
Ah....

One thing: does Heredoc allow multiple semicolons?

My code has and needs to have semicolons and I did:

Expand|Select|Wrap|Line Numbers
  1. $MyVariable = <<<EOD
  2. <?php
  3. $myfile = "$xxx.txt";
  4. $work = fopen ($myfile, "r");
  5. $hits = fread ($work, filesize($myfile));
  6. fclose ($work);
  7. echo "xxxx: <b>$hits</b>";
  8. ?>
  9. EOD;
and it doesn't work.

Can you help?

===============

Sorry, it Does work.
Jul 10 '10 #6
Samishii23
246 100+
Try using file_get_contents()
It would be a little easier on the eyes.
Jul 12 '10 #7
londres9b
106 100+
@Samishii23
Thanks! It's much simpler this way
Jul 12 '10 #8

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

Similar topics

5
by: golfer1212 | last post by:
I wrote this program and it seems to work but I have to hit the enter key twice before the else statement will execute. I have no idea what the problem is? Here is the code: #include...
148
by: onkar | last post by:
Given the following code & variable i . int main(int argc,char **argv){ int i; printf("%d\n",i); return 0; } here i is allocated from bss or stack ?
1
NeoPa
by: NeoPa | last post by:
Problem Description : In VBA there is an option to enforce declaration of variables in your code. With this set, any reference to a variable that has not been previously declared (Dim; Private;...
13
by: Ramon F Herrera | last post by:
I am writing a program that generates source code. See a snippet below. My question is about the use of that growing 'code' variable. Is it efficient? Is is recommended for this case? The code...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.