variable is blank, how to replace with something else?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PHP Nubsauce
Forum Newbie
Posts: 2
Joined: Sat Jun 07, 2008 10:22 pm

variable is blank, how to replace with something else?

Post by PHP Nubsauce »

I have

$username = user_name
$firstname = first_name
$lastname = last_name
addedby = $firstname' . '$lastname


If firstname and lastname are both blank, I'd like addedby to be username.

Any help would be greatly appriciated.

Thanks.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: variable is blank, how to replace with something else?

Post by Benjamin »

Hello. Please reference:

Control Structures
Comparison Operators
hansford
Forum Commoner
Posts: 91
Joined: Mon May 26, 2008 12:38 am

Re: variable is blank, how to replace with something else?

Post by hansford »

Hard to tell what you are trying to accomplish but I'll post this:
a varibale is set like:
$firstname = "Bill"
$lastname = "jacobs";
or, if you're using $_POST like this: $firstname = $_POST['first_name'];
if you want to assign one variable to another you can do:
$username = $firstname';
$username now holds the value "bill";
if you want to string 2 variables together you can do:
$addedby = $firstname . $lastname;
$addedby now holds the value "billjacobs";
wow thats ugly! lets try that again.
$addedby = $firstname . " " . $lastname;
$addedby now holds the value "bill jacobs";
Johnlbuk
Forum Newbie
Posts: 19
Joined: Mon Sep 10, 2007 3:01 pm

Re: variable is blank, how to replace with something else?

Post by Johnlbuk »

Once you have the variables firstname, lastname and username you can just run and if statement. In the example, the firstname etc are being posted from a form.

Code: Select all

 
<?php
$firstname = $_POST['first_name'];
$lastname = $_POST['last_name'];
$username = $_POST['user_name'];
 
if (($firstname=="") AND ($lastname=="")) {
$addedby = $username;
}  else {
$addedby = $firstname . " " . $lastname;
}
?>
 
John
Post Reply