Differences between printf, print, echo, sprintf,vprintf, vfprintf, vsprintf in PHP

WTF!!! Look  make no mistake about it,  I do like PHP but why do we have so many different ways to output strings. It is confusing especially nowadays when  a programmer is expecting to know more than one language.  As I move from one language to another i keep forgetting the nuances between all of them.

I wish they could just create just one construct and then we could just use flags to access specific functionalities.

So what are the differences between printf, print, echo, sprintf,vprintf, vfprintf and  vsprintf  in PHP?

According to php.net here are the  differences. At least they are all on one page for quick reference.

print :

  • Output a string onto the screen – Also “print” is not a real function so you are welcome to remove the parentheses.
  • Because this is a language construct and not a function, it cannot be called using variable functions
  • Always returns a 1

echo :

  • Output one or more strings
  • Also not a real function but a language construct
  • Does not returns anything so its cannot behave as a function.

printf :

  • Output a formatted string
  • Returns the length of the outputted string

sprintf:

  • Returns a formatted string
  • Returns a string produced according to the formatting string format

vprintf:

  • Returns a formatted string
  • Operates as printf() but accepts an array of arguments, rather than a variable number of arguments.
  • Display array values as a formatted string according to format

vsprintf:

  • Display array values as a formatted string according to format
  • Operates as sprintf() but accepts an array of arguments, rather than a variable number of argument

vfprintf :

  • Write a formatted string to a stream
  • Write a string produced according to format to the stream resource specified by handle
  • Returns the length of the outputted string

Reference: http://www.php.net/manual/en/ref.strings.php

Avoid SSH Timing Out

This one is really simple but it seems that i have to search for it every time. Note that keeping the connection alive is not really safe. Add these lines to your ssh_config

KeepAlive yes
ClientAliveInterval 120

Once you done you can comment the line out

#KeepAlive yes
#ClientAliveInterval 120

Installing phpMyAdmin securely on Centos, RedHat or Fedora

1. Make sure that you can access your server securely

https://yourserver.com

If you cannot access your server securely, let’s go and setup SSL on your server.

Again, I assume you are using a REDHAT based distribution.

  • Find if OpenSSL is running: “openssl version”
  • If OpenSSL is not running just install it with Yum : “yum install openssl”

Install a certificate by following the instructions in the following website:

(I wanted to write an article about SSL but this websites does a good job)

http://www.akadia.com/services/ssh_test_certificate.html

Your website should now be secure then it is time to install phpMyAdmin.


2. Unzip your phpMyAdmin to your virtual directory

Then browse to the virtual directory.

http://yourserver.com/phpmyadmin or wherever you put it.

Use the setup script to help you set up the config file;

http://yourserver.com/phpmyadmin/setup

Personally I do not like the setup script; I like to edit the config file manually.

When you done with the setup script, make sure you delete the “SETUP” folder.


3. We might also want to create a .htaccess file to force the db administrators to always access

phpMyAdmin securely.

Here is the content of the .htaccess files:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

4. Voila.


References:

http://www.phpmyadmin.net/documentation/Documentation.html#setup_script

http://www.akadia.com/services/ssh_test_certificate.html

http://www.cyberciti.biz/tips/howto-apache-force-https-secure-connections.html

Delete a User in Sugar CRM

If you have used SugarCRM, you probably have found out by now that there is no easy way to eradicate a user from your database (Check the SugarCRM forum).  I am not sure why this is not an option in the community, honestly, I have not seen the paid version so i do not know if the paid version provides such option.

I have created this stored procedure to help me out and hopefully it will help  somebody else:

1.   First,  you do not want to start deleting. You must first assigned any objects owned by the olduser to another user. To do that you must know the user id of both users (users table find the id column).  Just copy and paste and run the code below on MySQL to create the stored procedure. Then Call the stored procedure using the ids.

something like : Call DeleteReplaceUser (olduser, newuser)

2.   Once you have assigned all objects owned by the disgruntled employee (for example) to the other employee, you can now safely delete the user.

DELIMITER $$

CREATE DEFINER=`admin`@`` PROCEDURE `DeleteReplaceUser`(olduser varchar(20), newuser varchar(20))
BEGIN
-- assigned objects owned by old user to the new user
update accounts set assigned_user_id=newuser where assigned_user_id = olduser;
update address_book set assigned_user_id=newuser where assigned_user_id = olduser;
update bugs set assigned_user_id=newuser where assigned_user_id = olduser;
update calls set assigned_user_id=newuser where assigned_user_id = olduser;
update campaigns set assigned_user_id=newuser where assigned_user_id = olduser;
update cases set assigned_user_id=newuser where assigned_user_id = olduser;
update contacts set assigned_user_id=newuser where assigned_user_id = olduser;
update dashboards set assigned_user_id=newuser where assigned_user_id = olduser;
update emails set assigned_user_id=newuser where assigned_user_id = olduser;
update folders_subscriptions set assigned_user_id=newuser where assigned_user_id = olduser;
update import_maps set assigned_user_id=newuser where assigned_user_id = olduser;
update leads set assigned_user_id=newuser where assigned_user_id = olduser;
update meetings set assigned_user_id=newuser where assigned_user_id = olduser;
update opportunities set assigned_user_id=newuser where assigned_user_id = olduser;
update project set assigned_user_id=newuser where assigned_user_id = olduser;
update project_task set assigned_user_id=newuser where assigned_user_id = olduser;
update prospect_lists set assigned_user_id=newuser where assigned_user_id = olduser;
update prospects set assigned_user_id=newuser where assigned_user_id = olduser;
update saved_search set assigned_user_id=newuser where assigned_user_id = olduser;
update sugarfeed set assigned_user_id=newuser where assigned_user_id = olduser;
update tasks set assigned_user_id=newuser where assigned_user_id = olduser;
update user_preferences set assigned_user_id=newuser where assigned_user_id = olduser;
update users_last_import set assigned_user_id=newuser where assigned_user_id = olduser;

-- Now You are ready to delete the olduser
delete from users where id = olduser;

END$$

reference :

http://dev.mysql.com/tech-resources/articles/mysql-storedproc.html

http://www.sugarcrm.com/forums/showthread.php?t=4896