php random.txt

They have: 5,633 posts

Joined: Jan 1970

Can someone give me a extreamly simple script for opening and reading from a random .txt document in php? php.net doesnt seem to be as helpfull as mdsn

They have: 3 posts

Joined: May 2005

hi there

use below code

<?
srand(time());
$random = (rand()%9);
print("random number between 0 and 9 is: $random");
?>
'

or want to creat... rand number between 10 to 15 use below

<?php
$numb
= rand(10,15);
echo(
$numb);
?>
'
Result: 11

May be it will help you

webshome

WebMastersHome.com - Add a post and Earn $.10
HostingTalk.in - Advertise you hosting company

They have: 5,633 posts

Joined: Jan 1970

now what about opening and reading from a text file. I am more consirned about that.

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

PHP.net > Documentation (English) > Function Reference > Filesystem Functions.

In particular, look into file_get_contents(), as well as the functions listed under the See Also heading. Read the comments too.

Typically, file_get_contents is used thus:

<?php
// store contents of /path/to/file in $myfile as a string
$myfile = file_get_contents(\"/path/to/file\");
?>

The Function Reference is invaluable. This is the section of the Documentation you should be spending most of your time in.

They have: 5,633 posts

Joined: Jan 1970

A single function?!?!?!
Thats practical but not versatile.
I'll stick with asp as my main language.

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

bja888 wrote: A single function?!?!?!
Thats practical but not versatile.
I'll stick with asp as my main language.

Huh? This is what you asked:

bja888 wrote: Can someone give me a extreamly simple script for opening and reading from a random .txt document in php?
...
now what about opening and reading from a text file. I am more consirned about that.

That is precisely what that function does (opening a .txt simply).

Versatility? In what sense? What options do you need? What are you trying to do?

There are many ways of handling files: Assassin provides one more example.

Did you read the documentation I linked to?

He has: 77 posts

Joined: Apr 2005

well you could go through:

<?PHP
$file = "random.txt";
$handle = fopen($file, "r+");
rewind($handle);
$contents = readfile($handle);
fclose($handle);
?>

But you'd be wasting a lot of time if all you wanted to do is read the file.

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

How rude bja... What you wanted was a simple way to open a text file and that's what you were told - what exactly is wrong with PHP's functionality?!!!

And, as shown by Assassin there is more than one way to do it, but why bother taking the convoluted route when PHP has a shortcut built in?

PHP is more popular than ASP for a reason you know Laughing out loud

a Padded Cell our articles site!

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

You know, just for giggles, I went to msdn.com, and using their search, looked for how to open a random .txt file in asp.

Only result that came up was something about service pack 3.

Here is what I see the problem being:

You are used to MSDN's site, and where to find what you are looking for, PHP.net, you are not familiar with, so it doesn't seem as friendly becasue you cannot easily find what you are looking for.

You know exactly what you are wanting the script to do, yet you posted a generalized request here. The responce to your generalized question didn't get you the detailed results you are looking for, so you seem to have the ideal that PHP sucks and it is better to stick with ASP.

People are going to read your attitude on this, and are going to be less likely to offer help in the future as to avoid the same ungratefull responces.

Its a fact that trying a new language is difficult when you are used to soemthing else. That is why I stick with PHP for web and VB for standalone software. I'm used to both and know what I'm doing, and can easily find help.

I know it gets frustrating, you are trying to do something a new way, when in your mind you know "I can do it the other way". That is how I am when it comes to switching from suing tables over to CSS for layout. I start trying to do it in CSS, then time gets running short, and deadlines come up, and well, I'm a bad boy and throw in a table.

My advise, try to have a little more patience. Ask more specific questions. Spend the time to browse through functions at php.net. I still go there when I get a chance and look up functions I never used before. There are several I came across that way that gave me ideas of better ways to write code.

If you are in a hurry to get this script running, then I would say do it in ASP, don't try learn something new when it is something needed ASAP. (like the way I tried learning CSS, i didn't get anywhere, and didn't retain what little I did learn).

Good luck on your learning of PHP. I am more than willing to help those who appricate the help and want to learn it, and have the patience for learning it.

-Greg

They have: 5,633 posts

Joined: Jan 1970

Thank you for answering my question. You told me exactly what I needed to know.
I am only doing this in PHP bacause thats the only language mt clients server supports. I would love to do it in asp.net.
I have never heard of php.net. Sounds like another stupid idea from microsoft. Php is used way more than asp. I am more comfortable with asp so I will stick with what I know. Smiling

He has: 77 posts

Joined: Apr 2005

http://www.php.net is the official PHP website with all of the documentation. As far as I know, it is not a language like I believe ASP.net is. Either way, PHP is more powerful and the 5.0 on up versions of it are actually FASTER than ASP. I'm not going to tell you that you should be switching over because that would be excedingly difficult and probably unnecessary, but you can't knock what is currently the dominating server side scripting language on the net just because you use another one.

They have: 5,633 posts

Joined: Jan 1970

well i never hear anything about php being faster....
You do know that asp.net is compiled right?
I would think a compiled app would work better than a text based one dont you think?

Still though... people should use what their comfortable with. I just feel that the way asp works feels more.... well, versatile.

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

bja888 wrote: well i never hear anything about php being faster....
You do know that asp.net is compiled right?
I would think a compiled app would work better than a text based one dont you think?

Not necessarily. If it is compiled to bytecode rather than native code, it could be slower than non-compiled interpreted code -- it depends on the interpreter in both cases. Case in point: Java vs. Python (Java can be slower).

Edit to add: Zend Encoder and Optimizer tools can be used to compile PHP pages for additional performance gains. Smiling

bja888 wrote: Still though... people should use what their comfortable with. I just feel that the way asp works feels more.... well, versatile.

Of course it would feel more versatile. You're familiar with ASP whereas you're not with PHP. Smiling

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.