SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Beginner question - Can there be a php code block within a php code block?

    Hi everyone,

    Can there be a php code block within a php code block?

    Example:

    PHP Code:
    <?php

         
    <?php

         ?>

    ?>
    Thank you
    Jaysone

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    No, why ever would you need that?

    A PHP opening tag opens PHP parsing. Opening it again without previously closing it would have no effect - other than a good Syntax error.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much,

    I was doing some crazy stuff with arrays like this:

    PHP Code:
    <pre><?php  

    $cars 
    = array('BMW M5''Ferrari F430''Ferrari F50''Nissan Skyline GTR 35');

    print_r($cars);

    ?></pre>
    then i was thinking, how can we echo out the <pre></pre> tags and insert the print_r in between the <pre> tags. Just experimenting.

    Thank you again
    Last edited by kennethjaysone; Mar 28, 2009 at 18:24. Reason: Error in code
    Jaysone

  4. #4
    SitePoint Guru Dashman's Avatar
    Join Date
    Jan 2006
    Location
    Manchester, UK
    Posts
    627
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi ...

    A useful and very basic function I use in all my work, mainly for debugging goes as follows:
    PHP Code:
    function printArray($array){
        print 
    "<pre>".print_r($arraytrue)."</pre>";
    }
    $cars = array('BMW M5''Ferrari F430''Ferrari F50''Nissan Skyline GTR 35');

    printArray($cars); 
    That way, it is just as easy to print out a nicely formatted array as it is to write:
    PHP Code:
    print_r($cars); 
    Hope this helps.

    D

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Then you'd do it like:
    PHP Code:
    $cars = array('BMW M5''Ferrari F430''Ferrari F50''Nissan Skyline GTR 35');
    echo 
    '<pre>';
    print_r($cars);
    echo 
    '</pre>'
    Edit:

    Or the solution above
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Though you probably want to do...
    PHP Code:
    function printArray($array){
        print 
    "<pre>".htmlspecialchars(print_r($arraytrue))."</pre>";


  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could also stop and restart the php tags like this:

    Code PHP:
    <?php  
     
    $cars = array('BMW M5', 'Ferrari F430', 'Ferrari F50', 'Nissan Skyline GTR 35');
     
    ?>
    <pre>
    <?php
     
    print_r($cars);
     
    ?>
    </pre>
    <?php
    //ect...
    ?>

  8. #8
    SitePoint Guru Dashman's Avatar
    Join Date
    Jan 2006
    Location
    Manchester, UK
    Posts
    627
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats very true what sk89q .... nice one :-)

  9. #9
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Awesome guys!

    How do you come with these ideas?

    Does it come to you naturally the more you practice, practice, practice or is it the experience you've gained over the years?

    Thank you
    Jaysone

  10. #10
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Practice!

    And you'll find it isn't years... It can come in weeks if you try hard enough.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •