Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need Some PHP Help!
#1
First off, how can I store some PHP variables into a file? Maybe like:

$variable = ("/var/www/html/whatever/whatever.txt");

Also!

I'm having a problem(More important that above).
Running the script in the shell, it works fine, it'll print both variables, however on the web, in both firefox and safari, it'll only show one variable.

The code in the php script that outputs it is:
print(" The sun is $sunupdown up and the weather is currently $condition ");
?>

In the shell I get
The sun is not up and the weather is currently Partly Cloudy

On the web I get:
The sun is up and the weather is currently Partly Cloudy


Any ideas?
Thanks
Reply
#2
microchip13 Wrote:
-------------------------------------------------------
> First off, how can I store some PHP variables into
> a file? Maybe like:
>
> $variable =
> ("/var/www/html/whatever/whatever.txt");


How about making the file itself be PHP code that assigns the variables.
Then execute a
require "variables.php";
line in your PHP code to read in the file and execute it there to assign the variables.

The file variables.php would then be something like:
$variable = "my dog has fleas";
$another = 12;
?>

Reply
#3
I don't really get that?

The variables are dynamic variables.

The variables will only be updated once an hour, but throughout the hour, they will need to be read from the cache(Thus the need to write them somewhere).

I tried using fopen and fwrite, which works, kinda.
When it writes, it adds onto it, not copies over it, and it's having problems reading it.
I'm using:

$handle1 = fopen('/tmp/sunupdown.txt', 'a');
fwrite($handle1, $sun);

then to call it:

$variable123 = exec("cat /tmp/sunupdown.txt");
print(" The sun is $variable123 up");

The file sunupdown right now looks like
notnotnotnotnotnotnot
Reply
#4
microchip13 Wrote:
-------------------------------------------------------
> I tried using fopen and fwrite, which works,
> kinda.
> When it writes, it adds onto it, not copies over
> it,

> $handle1 = fopen('/tmp/sunupdown.txt', 'a');
> fwrite($handle1, $sun);
>

The 'a' means to append (add to) when writing to the file.
Maybe you want 'w' for a plain old writing to the file to overwrite the previous contents.
http://us2.php.net/manual/en/function.fopen.php

My solution outlined above would involve two parts.
Part 1. Create the file. You can do this either manually once for static variables or let your code create it using fopen() and fwrite(). Instead of plain data, you would write out a series of text lines that are the PHP code.
Part 2. Read in the file. Since the file is PHP, you could read it in and assign lots of variables in one operation using a 'require' statement as illustrated above.

Your method of writing raw data directly to a file is useful if you want to assign only one variable at a time. My method works for lots of variables stuffed into one file. I am sure there are other methods too that fall into a middle ground between the two we are discussing here. For example storing the data in an XML file is another option but probably more complicated than you need.
Reply
#5
microchip13 Wrote:
-------------------------------------------------------
> $variable123 = exec("cat /tmp/sunupdown.txt");

I'd use fread() instead. I avoid any calls to system functions like that if there is a built-in PHP function that accomplishes the same thing.

http://us3.php.net/manual/en/function.fread.php


Reply
#6
I would be using get_file_contents, unfortunatly, we're having some problems with Apache and such.

As of now, unfortunatly, it's based off quite a few shell things, alot of which is for advanced string manipulation.


I'll give that a try and see how it goes.

Thanks
Reply
#7
You'd be much better off using the built-in string manip functions (preg_replace, for example). There is a VERY heavy toll that must be paid every time you shell out.

- Shadow
Reply
#8
Yea, so I've heard by our local PHP whiz.

In It's makeshift state, it's not too bad, It works, I just can't get it to not read from the cache right now.


I'm assuming PHP has an equivalent to grep?
Reply
#9
microchip13 Wrote:
-------------------------------------------------------
> I'm assuming PHP has an equivalent to grep?

How about preg_grep() ?

You can learn about it and the rest of PHP here:
http://us3.php.net/manual/en/function.preg-grep.php
Reply
#10
Store the data into a mysql database...who knows? Maybe you'll want to view the weather at certain times in the future. The thing runs fairly fast, and it has a great interface with PHP. Install phpmyadmin and you can easily update your SQL database files.

GL.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)