Problems with strings in PHP (not adding two strings together properly) - php

if we say:
$termstringDomain1 = "something";
$url = "somethingelse";
$url = 'http://' . $termstringDomain1 . '/' . $url;
the result if gives me is: http:///somethingelse instead of http://something/somethingelse
so basically it is ignoring $termstringDomain1
Any idea why?

I am unable to reproduce this issue:
$foo = "foo";
$bar = "bar";
$bar = "sayFoo: " . $foo . ", sayBar: " . $bar;
print $bar; // 'sayFoo: foo, sayBar: bar'
Your problem is likely elsewhere. If I copy/paste what you provided, I get the following:
http://something/somethingelse
Check your variable casing. $domain1 is not the same as $Domain1.

Can you try putting echo infront of all the declarations, to try to track the problem down?
echo $termstringDomain1 = "something";
echo $url = "somethingelse";
echo $url = 'http://' . $termstringDomain1 . '/' . $url;

Related

How to combine two PHP variable names to call one?

I have multiple variables:
$Variable1 = '5/5/15';
$Variable2 = '6/13/76';
$Variable3 = '5/8/15';
...
I have an iteration variable:
$Iteration1 = 1;
while($Iteration1<=3){
echo "$Variable" . $Iteration1;
$Iteration1++;
}
desired result:
5/5/15
6/13/76
5/8/15
The Problem
Your current code echo "$Variable" . $Iteration1; tries to echo the value of the variable $Variable, which doesn't exist, and concatenate $Iteration1.
The Solution
What you want to do is build a string, "Variable" . $Iteration1 (e.g., $Variable2), then get the value of the variable with that name. This is called a "variable variable." You do this by writing ${string_you_want_to_create}, as in ${"Variable" . $Iteration1}.
Example Code For your Problem:
$Variable1 = '5/5/15';
$Variable2 = '6/13/76';
$Variable3 = '5/8/15';
$Iteration1 = 1;
while ($Iteration1 <= 3) {
echo ${"Variable" . $Iteration1} . "\n";
$Iteration1++;
}
Output:
5/5/15
6/13/76
5/8/15
Note: You could also do this in two steps, like this:
$variableName = "Variable" . $Iteration1;
echo $$variableName; // note the double $$
Try like this
echo ${"Variable" . $Iteration1};
Try this in loop
$var = 'Variable'.$Iteration1;
echo $$var.'<br>';

php variable variables to access property of an object

I want to store an property->object name in a table and access in code.
What is the proper way to accomplish the following.
$patient = new stdClass();
$patient->patient_number = '12345';
$cls = 'patient';
$prp = 'patient_number';
echo 'test1 = ' . $patient->patient_number . '<br>';
//--- this prints 12345 as expected;
echo 'test2 = ' . $$cls->patient_number . '<br>';
//--- this print 12345 as expected;
echo 'test3 = ' . $$cls->${$prp} . '<br>';
//--- this generates undefined variable patient_number;
Use this:
echo 'test3 = ' . ${$cls}->{$prp} . '<br>';

php echo statement inside $output

I have the following php code:
$skizzar_masonry_item_width = $masonry_item_width;
$skizzar_masonry_item_padding = $masonry_item_padding;
$skizzar_double_width_size = $masonry_item_width*2 +$masonry_item_padding;
$output .= '<style>.skizzar_masonry_entry.skizzar_ma_double, .skizzar_masonry_entry.skizzar_ma_double img {width:'.$skizzar_double_width_size.'}</style>';
return $output;
For some reason though, the value of $skizzar_double_width_size is not being added into the $output - is there a way to echo a value in an output variable?
As #Rizier123 mentioned, ensure you initialise any string variables before trying to append to them.
$var = '';
$var .= 'I appended';
$var .= ' a string!';
I would also like to strongly discourage you from using inline styles as well as generating them with inline PHP. Things get very messy very quickly.
In a situation like this you need to check that all the variables you are using in the calculation are valid before you panic.
So try
echo 'before I use these values they contain<br>';
echo '$masonry_item_width = ' . $masonry_item_width . '<br>';
echo '$masonry_item_padding = ' . $masonry_item_padding . '<br>';
$skizzar_masonry_item_width = $masonry_item_width;
$skizzar_masonry_item_padding = $masonry_item_padding;
$skizzar_double_width_size = $masonry_item_width*2 +$masonry_item_padding;
echo 'after moving the fields to an unnecessary intemediary field<br>';
echo '$skizzar_masonry_item_width = ' . $skizzar_masonry_item_width . '<br>';
echo '$skizzar_masonry_item_padding = ' . $skizzar_masonry_item_padding . '<br>';
echo '$skizzar_double_width_size = ' . $skizzar_double_width_size . '<br>';
$output .= '<style>.skizzar_masonry_entry.skizzar_ma_double, .skizzar_masonry_entry.skizzar_ma_double img {width:'.$skizzar_double_width_size.'}</style>';
echo $output;
This should identify which fields are causing you problems.
Also while testing always run with display_errors = On It saves so much time in the long run.

How can I find everything until something else in PHP and return that as a string?

I want to return these unique values from my URLs
394_black
500_mono2
The URLs:
/shop/swarovski/394_black_the-reagan-swarovski-maxi-dress
/shop/celeb/500_mono2_the-reagan-swarovski-maxi-dress
Can I do this in PHP?
Pseudo Code
$url = get url
$firstvar = after 3rd / and after second _ save text (500_mono2) from $url
$secondvar = using $firstvar return only first 3 numbers
//substr($firstvar, 0, 3); (I think this will be it)
Updated my Question above, easier to understand
<?php
$url = '/shop/celeb/500_mono2_the-reagan-swarovski-maxi-dress';
$urlparts = explode('/', $url);
list($var1, $var2, $var3) = explode('_', end($urlparts));
echo $var1 . '<br>' . $var2 . '<br>' . $var3;
?>

How to change part of an url in php?

I am new to PHP and I am trying to work on this one problem where the middle of a URL changes.
For instance using this fake URL,
$url = 'http://www.bobsplace.com/events/georgetown/12354544233123';
I need to be able to substitute "georgetown" out with a random variable.
So I guess I need to like slice the URL after events, have a variable where "georgetown" would be, and then have the rest of the URL continue after it. I hope I am making as much sense as possible.
Alright,
I tried all these and still go errors, but most likely it is due to my current coding. I am entering the entire thing in now.
<?
// This URL only shows future events
$url = 'https://graph.facebook.com/192674347443031/events?access_token=153420758043439|66d692f0e73ad17d939d9d9c-1045402872|8VHLfMb3gYY0p9fnT7wMS8Q9Krc&expires_in=0&since=yesterday' ;
$event_id = get_post_custom_values('facebook_id');
$url = str_replace('/192674347443031/', '/' . $event_id . '/', $url);
$json = file_get_contents($url) ;
$data = json_decode( $json,true) ;
//var_dump($data) ;
$sorted_array = array_reverse($data['data'], TRUE) ;
foreach ($sorted_array as $key => $value) {
//print_r($value) ;
?>
<? echo "<li><a href='http://facebook.com/event.php?eid=". $value['id'] . "' target='_blank'>" ;
echo "<h2>" . $value['name'] . "</h2>" ;
echo "</a>" ;
echo " (" . $value['location'] .") <br />" ;
echo date("F j, Y, g:i a" , strtotime($value['start_time'])) . " to " . date("F j, Y, g:i a" , strtotime($value['end_time'])) ;?>
<div class="moreinfo">
<? echo "<a href='http://facebook.com/event.php?eid=". $value['id'] . " ' target='_blank'>" ;
echo "<b>" . 'More info' . "</b>" ;
echo "</a>" ;?>
</div>
<? echo "</li>";
}
?>
The errors I keep getting are now saying my array_reverse() should be an array. And also invalid argument foreach()
Now this is not the entire code on that page, but this is the snippet that is giving me problems.
you can do :
$url = 'http//:www.bobsplace.com/events/georgetown/12354544233123';
$my_var = 'foobar';
$url = str_replace("georgetown", $my_var, $url );
or :
$url = 'http//:www.bobsplace.com/events/georgetown/12354544233123';
$my_var = 'foobar';
$temp = explode( '/' , $url );
$temp[4] = $my_var;
$url = implode( '/' , $temp );
First one will replace by name , second one by position in URL.
I think you're trying to say that you want to replace the text "georgetown". If that's correct, the following code should work (make sure you've defined $replace to what you want to replace it with):
$url = str_replace('/georgetown/', '/' . $replace . '/', $url);
$variable = 'place'; //e.g. georgetown
$url = 'http:www.bobsplace.com/events/'.$variable.'/12354544233123';
I think this is what you're asking for.

Categories