Tagged: PHP Toggle Comment Threads | Keyboard Shortcuts

  • Jay Versluis 3:10 pm on June 17, 2017 Permalink | Reply
    Tags: PHP   

    Categories: Linux ( 96 )

    How to read command line parameters in PHP Shell Scripts 

    We can access parameters passed via the command line in our PHP shell scripts. Those are stored as an array in the variable $argv. Consider this:

    echo var_dump($argv);
    echo "\n";
    if ($argv[1] == 'x') {
      echo "The parameter is x.";
    } else {
      echo "The parameter was something else.";

    The first part of the script prints out all parameters that have been given, while the second part checks if the parameter was “x” or not. Note that the first item in the array ($argv[0]) will be the the first item on the command line, i.e. the file name and path to this very script. $argv[1] is the first parameter, $argv[2] the second, and so forth.

    We can call the script with

    script.php x

    to give it one parameter, or with

    script.php x y z

    to give it three parameters.

  • Jay Versluis 5:02 pm on March 13, 2014 Permalink | Reply
    Tags: , PHP   

    Categories: WordPress ( 138 )

    How to test if your theme is a Child Theme 

    You may need to know which of your themes are child themes, or in fact if the current theme you’re using is a parent or a child theme.

    Here’s how you can test both options.

    The following code snippet will iterate through all themes that are currently installed and displays the title and if it is a child theme or not:

    $allThemes = wp_get_themes();
    echo '<ol>';
    foreach ($allThemes as $theme) {
    	echo '<li>';
    	// print the theme title
    	echo $theme->get('Name');
    	// determine whether it's a child theme or not
    	if ($theme->parent() == false) {
    		echo ' is not a Child Theme.</li>';
    	} else {
    		echo ' is a Child Theme</li>';
    echo '</ol>';

    First we grab an array of installed themes using wp_get_themes(). Each item is an object of WP_Theme and has many helpful methods. Its method parent() will return false for non-child themes, or the parent theme if it is in fact a child theme.

    Next we test if the output is false, and if so print a status accordingly.

    Is the current theme a child theme?

    Here’s how we can check it:

    // is the current theme a child theme?
    $currentTheme = wp_get_theme();
    if ($currentTheme->parent() == false) {
    	echo 'The current theme is not a child theme.';
    } else {
    	echo 'The current theme is a child theme';

    Here we employ very much the same, except for the first line in which we grab only a single object which defaults to the current theme.

    Check out all the other bits of info this class can provide.

  • Jay Versluis 4:28 pm on March 13, 2014 Permalink | Reply
    Tags: PHP   

    Categories: Bookmarks ( 18 )

    Handy Cheat Sheet for PHP File Operations – thanks to @davidwalshblog

  • Jay Versluis 2:36 pm on February 16, 2014 Permalink | Reply
    Tags: , , PHP   

    Categories: Plesk ( 70 )

    How to upgrade to MySQL 5.5 and PHP 5.4 in Plesk 11.5 (CentOS) 

    Plesk-LogoOne of my clients wanted to run a web application that required MySQL 5.5 instead of the default MySQL 5.1 that ships with Plesk at the time of writing. He needed it due to the better handling of UTF8 characters.

    I struggled to find clear instructions on how to do this – so here they are ๐Ÿ˜‰

    CentOS 5.x

    In CentOS 5 you can switch to MySQL 5.5 under Tools and Settings – Updates and Upgrades. The installer shows something like this:

    Screen Shot 2014-02-16 at 11.46.19

    Simply switch to MySQL 5.5 (packaged by Parallels) and hit install.

    CentOS 6.x

    Sadly the above convenience doesn’t exist in CentOS 6, so we’ll have to make use of the excellent Atomic repository again. In the past I’ve experienced instabilities in the way Plesk may react to non-standard repo packages – but it appears this is no longer an issue (or so I was told). Suffice to say proceed at your own risk and test, test and test again before using this in a production environment.

    Install access to the Atomic repo with this command:

    wget -q -O - http://www.atomicorp.com/installers/atomic | sh

    You’ll have to agree to some terms and conditions, after which you can simply use yum update to include some additional functionality that is not provided by CentOS or Parallels. Doing so will automatically update both MySQL and PHP for you.

    Restart the server when you’re finished for good measure.

    Check out the full article

    • Mihai 3:53 pm on February 25, 2016 Permalink | Reply

      Not worked here dunno why … other commands for centos 6 ? best regards

    • wblondel 9:07 pm on March 12, 2016 Permalink | Reply

      Thanks a lot for this guide ! It worked on CentOS 6.7 ๐Ÿ™‚

    • Gerard Dy 12:13 am on November 25, 2016 Permalink | Reply

      Hi Jay,

      If I want to upgrade to PHP 5.4+ only is that possible on CentOS 5.6 and Plesk 11.5.30? Do you have tutorials for that? Thanks.

      • Jay Versluis 7:55 am on November 25, 2016 Permalink | Reply

        Hi Gerard, yes it should be. It’s been a while since I’ve used CentOS 5 with Plesk 11.5, but from what I remember it’s as simple as installing the Atomic repo as described above, then issue “yum update php” and hopefully see a new version appear. Good luck!

  • Jay Versluis 7:18 am on February 8, 2014 Permalink | Reply
    Tags: PHP   

    Categories: Linux ( 96 )

    How to run PHP from the command line in Linux 

    php-med-transDid you know that you can run PHP directly from the command line? Here’s how:

    Running a command

    Let’s run a one-liner and see the output displayed in our console. We’ll use phpinfo() because we know it should generate a lot of text. We need to start our line with php, use the -r switch and specify our command in single quotes:

    php -r 'phpinfo();'

    That’s it. You should see a familiar long list of set variables and info about your PHP environment. Note that you have to specify the semicolon inside your brackets, just like you would at the end of any PHP command.

    You can also run several commands in a row:

    php -r 'echo "Hello "; echo "Jay";'

    Notice that when we use single quotes to encapsulate the command, we can use double quotes inside it.

    Running a script

    You can execute an entire php file from the command line too. Unlike other shell commands, when called with the php parameter there’s no need to change the file permissions. The file does not need to end with .php either.

    Consider the following simple script – let’s call it “test.php”. Note the use of the opening and closing php tags, just like we’d use them for web output.

    echo "Hello Jay!";

    We can run this script from our command line like this:

    php -f test.php

    You can also run the same script “Linux Shell Command Style” without specifying php first. You need to modify your script in two ways for this to work.

    First, we need to include the path to PHP at the beginning of the script, like we’d do with Python or BASH scripts. We do this so that our shell knows how to run the script. The UNIX shebang will look familiar:

    echo "Hello Jay!";

    We also need to change the permissions to allow execution, again like we’d do with other scripts.

    chmod +x test.php

    Now we can run it like this:


  • Jay Versluis 6:34 pm on December 29, 2011 Permalink | Reply
    Tags: , , , PHP   

    Categories: Linux, PHP ( 96 )

    What’s the full path to php in CentOS? 

    The path to PHP is


    This is good to know if you need to setup a cron job which triggers a PHP file. Calling it from a web browser directly is not a problem, but if you have to call it from the command line or as a scheduled task you need to call it with

    /usr/bin/php yourfile.php

    You can also use wget or cURL but that’s often not reliable.

    If your PHP file gives you an output (usually to the browser screen), your server will send you an email. If you;d rather this didn’t happen, direct it to nowhere like so:

    /usr/bin/php yourfile.php > /dev/null 2>&1

  • Jay Versluis 11:08 am on February 25, 2009 Permalink | Reply
    Tags: , PHP,   

    Categories: MySQL ( 19 )

    How to restore your database? 

    The Database is the heart of your WordPress installation. Simply backing up and restoring all your files via FTP is not enough – it only contains the actual WordPress “programme” if you will, your plugins and themes, and all your uploaded media files (like pictures). It does NOT however contain your posts, links and some of the configuration data. That’s where the MySQL Database comes into play.

    For any of these articles to make sense, you need to be familiar and have access to phpMyAdmin. Your hosting provider will gladly help you our there.

    If you have a fairly small database (up to 5MB), have a look at this:

    If you database is significantly larger, you need other means of uploading and importing it. I reccomend the sue of a PHP script, as detailed here:

    One sure PHP Script is BigDump:

    Obviously, before a RESTORE actually works, you would have had to BACKUP your database (naturally). Perhaps you would like to check out this article to find out how to do this:


  • Jay Versluis 3:13 pm on February 8, 2009 Permalink | Reply
    Tags: , PHP, Yahoo Pipes   

    Categories: PHP, Plugins ( 26 )

    How to add a Facebook Group Feed 

    I’ve come acoss this the other day, comes in handy as Facebook Groups don’t have a generic RSS feed. Here’s how you can display what’s new in your favourite groups within your WordPress Site:

    1. Make sure you have your Facebook Group ID handy (it’s the last 11 or so numeric digits after the ‘groupid’ tag in the URL). Check Jason Silver’s excellent Yahoo! Pipe script:
    2. http://pipes.yahoo.com/jasonsilver/facebookgroup
    3. This will give you an option to embed as badge in various formats (WordPress, Blogger, PHP Script and many others).


  • Jay Versluis 12:08 pm on February 7, 2009 Permalink | Reply
    Tags: , increase upload limit, PHP   

    Categories: PHP ( 26 )

    How to change the PHP Upload Limit 

    Usually your hosting package limits the amout of data you can upload to 2MB, which isn’t enough for most of us. It’s good for that 200k photo, but say you’re uploading a 4MB audio clip, a video or a large PDF, you’re stuck.

    Fret not my friends, for help is at hand! Put the following code in your .htaccess file and smoke it:

    php_value upload_max_filesize 100M
    php_value post_max_size 100M

    Instead of “100M” you can choose any limit you like, say 5M, 10M or whatever limit you like.

    If you can’t find the .htaccess file, it might either be that

    • your FTP client doesn’t display hidden files; make sure that it does
    • or it doesn’t exist (in which case, create a plain text file, upload it and name it .htaccess)

    If you’re having trouble, just download this file. Then simply unZIP it and upload the contents into your WordPress directory. Once it’s on the server, rename it to .htaccess and you’re done.

    Good luck ๐Ÿ˜‰

    • Joe White 6:46 pm on November 8, 2011 Permalink | Reply

      Thanks, Jay. I’m a bit confused as to why such a limit would be imposed. I’m using WordPress through a hosting service with unlimited (theoretically) memory. I basically just want the option of embedding MP3 and video files without having to link to something like Soundcloud (whose visual interface I don’t like.)

      Any other options for how to get around this 8 mb limit?


      • Jay Versluis 8:08 pm on November 8, 2011 Permalink | Reply

        I’m sure you hosting company can increase this for you – if not via .htaccess then maybe there’s another way. Having said that, shared hosting is a finely tuned ecosystem so I do understand that they want to keep the caps on how much RAM each user can use. Who are you hosting with? Unlimited is probably referring to disk space rather than system memory ๐Ÿ˜‰

        You can upload larger files via FTP and then link to them manually. It’s not as convenient as uploading them from the WordPress backend, but works with files as large as your web space allows. Search for FileZilla, that’s a free FTP client. Upload your files to wp-content/uploads for example and access them like this: http;//yourdomain.com/wp-content/yourfile.mp3

        Hope this helps ๐Ÿ˜‰

        • Dan 10:32 pm on December 8, 2011 Permalink | Reply

          Thank you Jay! I’ve been searching the internet for a solution. Editing the .htaccess file did not work for me either (I got the internal error message). I don’t know why I did not think to just upload it via an FTP program, but this makes perfect sense!

compose new post
next post/next comment
previous post/previous comment
show/hide comments
go to top
go to login
show/hide help
shift + esc