Tom10 Posted April 8, 2015 Share Posted April 8, 2015 Hi i am currently going through functions trying to learn more about php and i used the scandir() function, is there a way to get the output on the page without using print_r() ? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/295365-scandir/ Share on other sites More sharing options...
gizmola Posted April 8, 2015 Share Posted April 8, 2015 I'm not sure what you mean. In a web context (php being used with a web server) any output that is returned via echo or printf will be sent to the client as html by default. print_r, var_dump and var_export are special helper functions that will give you some sort of output even when you pass them an object or array. They are for quick and dirty debugging. echo is your standard way of returning data to the browser as html. Quote Link to comment https://forums.phpfreaks.com/topic/295365-scandir/#findComment-1508545 Share on other sites More sharing options...
Tom10 Posted April 9, 2015 Author Share Posted April 9, 2015 I tried this <?php $scan = scandir("/"); echo $scan; ?> And got an output Array Quote Link to comment https://forums.phpfreaks.com/topic/295365-scandir/#findComment-1508546 Share on other sites More sharing options...
Tom10 Posted April 9, 2015 Author Share Posted April 9, 2015 It's ok i figured it out foreach(preg_grep("#[^\.]#", scandir(getcwd())) as $contents) { if($contents != '.' | $contents != '..') { print($contents . ' <BR />'); } } Quote Link to comment https://forums.phpfreaks.com/topic/295365-scandir/#findComment-1508548 Share on other sites More sharing options...
gizmola Posted April 9, 2015 Share Posted April 9, 2015 Great. Also just a tip, Print and echo are the same function. Most people use echo, because it's shorter. Printf came over from the 'C' language and uses formatting placeholders and parameters. There are some cases when you want to control things like the exact format of a number where printf can be attractive, although there are often a number of different ways to accomplish the same thing in php, and for the most part it's personal preference. 1 Quote Link to comment https://forums.phpfreaks.com/topic/295365-scandir/#findComment-1508581 Share on other sites More sharing options...
Barand Posted April 9, 2015 Share Posted April 9, 2015 Great. Also just a tip, Print and echo are the same function. Most people use echo, because it's shorter. Sorry, Gizmola, but going to have to argue with you over that one. 1. print returns a value (always 1), echo does not (void function) 2. echo will accept multiple arguments whereas with print you have to use concatenation eg echo 'Today is ', date('Y-m-d'); // multi args works print 'Today is ', date('Y-m-d'); // multi args - error print 'Today is '. date('Y-m-d'); // concatenation works 2 Quote Link to comment https://forums.phpfreaks.com/topic/295365-scandir/#findComment-1508585 Share on other sites More sharing options...
gizmola Posted April 9, 2015 Share Posted April 9, 2015 Thanks Barand -- I should have said "practically". Also more good reasons why people use echo instead. 1 Quote Link to comment https://forums.phpfreaks.com/topic/295365-scandir/#findComment-1508590 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.