I have this function:
function my_function($value1, $value2, $value3 = null){
if($value1 || $value2 || $value3 == 'my_title' ){
echo '<title>'.$value2.'</title>';
}
}
my_function('my_css', 'my_title');
So as you can see I had to put in the echo $value2 becuase I cannot figure out how I would be able to use either $value 1,2 or 3. I was thinking this could be solved by doing something like this:
if($value1 || $value2 || $value3 == 'my_title' $title = $value1 || $value2 || $value3 ){
Is this possible?
I hope you can understand what im trying to achieve here.
---------------------------------------------------- SOLVED --------------------------------------------------------
Thanks to rohitarora for the best answer for this question.
i would like to share with everyone what you have helped me make.
if you have this function
function template_content($value1, $value2, $value3 = null){ global $title;
$value = array($value1, $value2, $value3);
for($i=0;$i<count($value);$i++)
{
if (strpos($value[$i],'title: ') === 0 ){
$title = explode("title: ",$value[$i]);
echo "\t", '<title>'.$title[1].'</title>', "
";
} else if (strpos($value[$i],'css: ') === 0 ){
$css = explode("css: ",$value[$i]);
echo "\t", '<link rel="stylesheet" href="'.$css[1].'">', "
";
} else if (strpos($value[$i],'js: ') === 0 ){
$js = explode("js: ",$value[$i]);
echo "\t", '<script src="'.$js[1].'"></script>', "
";
}
}
}
you can call this function
<!DOCTYPE html>
<html>
<head>
<?php template_content('css: http://google.com/style.css', 'title: hello', 'js: http://code.jquery.com/jquery-latest.min.js'); ?>
</head>
<body>
</body>
</html>
and you will get this amazing result
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://google.com/style.css">
<title>hello</title>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
</body>
</html>
function my_function($value1, $value2, $value3 = null) {
// make a array of all your values
$title = array($value1, $value2, $value3);
// for loop will run as the size of array like here there are three element in array value1,2,3
for ($i = 0; $i < count($title); $i++)
{
// check if the string is there in the ith element in array
if ($title[$i] == 'my_title')
{
echo '<title>'.$title[$i].'</title>';
}
}
}
my_function('my_css', 'my_title');
Do it in array, that's a better way.
$string = 'my_title';
switch ($string) {
case $value1 : $title = $value1; break;
case $value2 : $title = $value2; break;
case $value3 : $title = $value3; break;
}
echo '<title>'.$title.'</title>';
This is what you need :)
function my_function (){
$args = func_get_args();
foreach ($args as $arg){
echo '<title>'.$arg.'</title>';
}
}
my_function('my_css', 'my_title');
This function can take any number of params you give it, and will print every one inside tags
The statement
if ($value1 || $value2 || $value3 == 'my_title') {
// Some code
}
does not return the result that you might think it will: it is evaluated as follows: if either $value1
is true
(it is evaluated as it were a boolean), or $value2
is true
, or $value3
equals my_title
, then execute some code.
If you want to use either $value1
, $value2
or $value3
when it equals my_title
, use the following:
function my_function($value1, $value2, $value3 = null) {
$searchStr = "my_title";
$r = "";
switch ($searchStr) {
case $value1:
$r = $value1;
break;
case $value2:
$r = $value2;
break;
case $value3:
$r = $value3;
break;
}
echo '<title>'.$r.'</title>';
}
If you are allergic for switch statements (well, you really shouldn't!), then use this:
function my_function($value1, $value2, $value3 = null) {
$searchStr = "my_title";
$r = "";
if ($value1 == $searchStr) {
$r = $value1;
}
else if ($value2 == $searchStr) {
$r = $value2;
}
else if ($value3 == $searchStr) {
$r = $value3;
}
echo '<title>'.$r.'</title>';
}
Notice that it's better to return the result of a function rather than echoing it instantly.