Page 1 of 1

variable is blank, how to replace with something else?

Posted: Sat Jun 07, 2008 10:24 pm
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.

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

Posted: Sat Jun 07, 2008 10:39 pm
by Benjamin
Hello. Please reference:

Control Structures
Comparison Operators

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

Posted: Sun Jun 08, 2008 2:16 pm
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";

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

Posted: Sun Jun 08, 2008 3:07 pm
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