help a newbie pls.

Discussion in 'PHP' started by mk24, Nov 30, 2008.

  1. #1
    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:confused: please advise, thank you.
     
    mk24, Nov 30, 2008 IP
  2. bugcoder

    bugcoder Peon

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    your script dont have any error. may be the server you are uploading to doesnt support php. check your hosting service providers .
     
    bugcoder, Nov 30, 2008 IP
  3. misbah

    misbah Active Member

    Messages:
    265
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    68
    #3
    save your file to php extension like this phpIndex2.php
    most of server do not execute file with html extension
     
    misbah, Nov 30, 2008 IP
  4. mk24

    mk24 Banned

    Messages:
    36
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks misbah!! worked !!!
     
    mk24, Dec 1, 2008 IP
  5. mk24

    mk24 Banned

    Messages:
    36
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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
     
    mk24, Dec 1, 2008 IP
  6. misbah

    misbah Active Member

    Messages:
    265
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    68
    #6
    try this
    <?php
    $handle = fopen('test.txt', 'r');
    $contents = fread($handle, filesize('test.txt'));
    fclose($handle);
    
    $keywordlist = explode("\n", $contents);
    echo $keywordlist[0];
    ?>
    PHP:
     
    misbah, Dec 1, 2008 IP
  7. mk24

    mk24 Banned

    Messages:
    36
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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.
     
    mk24, Dec 2, 2008 IP
  8. khu84

    khu84 Active Member

    Messages:
    441
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #8
    khu84, Dec 3, 2008 IP