Hello. I am very new to PHP. I *do* read tutorials and such, and I don't know where I am going wrong here. I am just getting started, so please bear with me. I am trying the very basics here, echoing output to screen on a website. Here is my code, simply declaring 2 variables a and b, echoing them, as well as echoing some strings directly. <html> <head> <title>Untitled Document</title> </head> <body> <?php $a = 3; echo "the a varibale holds: "; echo $a; $b = "this is b"; echo "the b variable holds: "; echo $b; ?> </body> </html> Code (markup): I have uploaded it here: http://tropical6.com/phpTest/phpIndex2.html i get no output on screen!! where am i going wrong please advise, thank you.
your script dont have any error. may be the server you are uploading to doesnt support php. check your hosting service providers .
save your file to php extension like this phpIndex2.php most of server do not execute file with html extension
okay... now that i got that working, i moved on to the next step, where i have encountered a new problem.. here is my code: $keywords = fopen("keywords.txt", "r"); $keywordlist = strtok($keywords, "\n"); echo $keywordlist; Code (markup): I want to seperate the keywords.txt filed (stored in variable $keywords) by line breaks. So when i echo $keywordlist, i am expecting the first line of my keywords.txt file to be echoed, except i get: 'Resource id #1' as my output (which is not the first line of keywords.txt!!). Thanks for the help guys
try this <?php $handle = fopen('test.txt', 'r'); $contents = fread($handle, filesize('test.txt')); fclose($handle); $keywordlist = explode("\n", $contents); echo $keywordlist[0]; ?> PHP:
Once again, thank you misbah, it works. One question, In the manual it talks about how different operating systems use different symbols for line breaks. Is 'operating system' here talking about the users OS or the server OS? For example, if my server is a unix server, will it always be fine to use "\n" as the line break even if the user was on a macintosh for example? so I am now trying to modify the strings of the array that i created in the previous step, but I am having another problem. if i use $keywordlist[0] = str_replace("X", "Y", $keywordlist[0]); PHP: this works fine, when i echo $keywordslist[0] i get the string i want with X replaced with Y. But if i try to implement this in an loop to go through each element of the array, i get no output on screen. the page loads for a few seconds, then a blank screen, no output. Here is the code I am using in a for loop, i don't understand why it doesnt work. for ( $i = 0; $i <= count($keywordlist); $i += 1) { $keywordlist[$i] = str_replace("X", "Y", $keywordlist[$i]); } PHP: I have echo statements after the for loop for $keywordlist[0], [1], [2], ... but no output.
Also check out: http://codingtricks.blogspot.com for lots of exciting scripts you may learn, complex work made easy